使用多个元素解析XML

时间:2013-12-13 20:12:16

标签: c# xml xml-parsing .net-4.5

我发现很难解析下面的xml结构(使用c#.net 4.5)并存储在DB(Sql-Server)中。

只要每条记录有一个属性,我就知道如何解析,例如出版商;日期(年),但正如你从作者的结构中看到的那样,不止一个,这就是我在努力的地方。

我使用XmlNodeList,XmlNode和InnerText从每个元素中提取数据。

感谢您的时间和帮助

XML结构

<Books>
  <Book>
    <title></title>
    <date>
      <year></year>
    </date>
    <Link></link>
    <summary></summary>
    <publisher>
      <name></name>
    </publisher>
    <authors total="2">
      <author>
        <name></name>
      </author>
      <author>
        <name></name>
      </author>
    </authors>
  </Book>
</Books>

我现在已经附加了我正在使用的代码,除了(因为它具有多个不同于其他属性的属性)之外,它正常工作。

XmlDocument xml = new XmlDocument();
                xml.LoadXml(/*Here I pass the xml which has been stored in string variable. The structure of which has been in my original post*/);
                XmlNodeList xnList = xml.SelectNodes("/Books[@*]");
                foreach (XmlNode xn in xnList)
                {
                    XmlNodeList anodes = xn.SelectNodes("Book");
                    foreach (XmlNode node in anodes)
                    {
                        if (anodes != null)
                        {
                            titles.Add(node["title"].InnerText);  //titles is an list
                            summarys.Add(node["summary"].InnerText);//Summarys is an list
                            link.Add(node["Link"].InnerText);//link is an list
                            counter +=1;
                                }
                      }
                            XmlNodeList CNodes = xn.SelectNodes("Book");
                            foreach (XmlNode node2 in CNodes)
                            {
                                XmlNode example = node2.SelectSingleNode("date");
                                if (example != null)
                                {
                                    years.Add(example["year"].InnerText);//years is an list
                                }
                            }
                            XmlNodeList dNodes = xn.SelectNodes("Book");
                            foreach (XmlNode node3 in dNodes)
                            {
                                XmlNode example1 = node3.SelectSingleNode("publisher");
                                if (example1 != null)
                                {
                                    publishers.Add(example1["name"].InnerText);//publishers is an list
                                }
                            }

                           XmlNodeList fnodes = xn.SelectNodes("Book/authors/author/name");
                                foreach (XmlNode node5 in fnodes)
                                {

                                    authors.Add( node5 != null ? node5.InnerText : "");//authors is an list. For the first book i need to store in list[0] and next in list[1]

                                    }
                            }
        // Convert list to array
                            titles.ToArray();
                            summarys.ToArray();
                            urls.ToArray();
                            years.ToArray();
                            publishers.ToArray();
                            authors.ToArray();

1 个答案:

答案 0 :(得分:0)

我希望此代码能为您提供帮助:

            XDocument xDoc = XDocument.Load("asd");
            var book = (from b in xDoc.Descendants("Book")
                select new Book
                {
                    Title = b.Element("title").Value,
                    Year = b.Element("date").Element("Year").Value,
                    Link  = b.Element("link").Value,
                    Summary = b.Element("summary").Value,
                    Publisher = b.Element("publisher").Element("name").Value,
                    Authors = (from a in b.Element("authors").Elements("author")
                                   select a.Element("name").Value).ToList(),
                }).First();

图书课程:

class Book
{
    public string Title { get; set; }

    public string Year { get; set; }

    public string Link { get; set; }

    public string Summary { get; set; }

    public string Publisher { get; set; }

    public List<string> Authors { get; set; }
}

PS:XDocument类位于System.Xml.Linq命名空间