在XML文件的第二嵌套级别中解析元素

时间:2013-04-05 23:13:56

标签: xml c#-4.0 xml-parsing linq-to-xml

我正在开发一个小的可执行应用程序。该应用程序只是一个XML解析器,它将解析XML文件并将解析的数据存储到数据库中。因此,该应用程序将发挥作用的XML文件具有以下结构:

<?xml version="1.0" encoding="utf-8"?>
<events>
    <event>
        <book>Felicity Fly</book>
        <author>Christina Gabbitas</author>
        <bookImgUrl>http://www.whsmith.co.uk/Images/Products\957\255\9780957255203_t_f.jpg</bookImgUrl>
        <info>Christina Gabbitas will be signing copies of her new book, Felicity Fly. Books should be bought from WHSmith. Proof of purchase may be necessary</info>
        <date>25 May 2013</date>
        <startTime>10:30</startTime>
        <location>
            <name>WHSmith Brent Cross</name>
            <address>Brent Cross Shopping Centre</address>
            <city>London</city>
            <county/>
            <postcode>NW4 3FB</postcode>
            <tel>020 8202 4226</tel>
        </location>
    </event>
    <!-- many more events as above here -->
</events>

到目前为止,这是我在代码解析逻辑方面所拥有的。

namespace XMLParser
{
    public class Parser
    {
        static void Main(string[] args)
        {
            var path_to_xml = "data.xml";
            var xdoc = XDocument.Load(path_to_xml);
            var events = from e in xdoc.Descendants("event")
                         select new {
                             Book = (string)e.Element("book").Value,
                             Author = (string)e.Element("author").Value,
                             BookImgUrl = (string)e.Element("bookImgUrl").Value,
                             Info = (string)e.Element("info").Value,
                             Date = (string)e.Element("date").Value,
                             Time = (string)e.Element("startTime").Value,
                             // stuck here
                         }
        }
    }
}

我遇到位置节点时遇到困难,我不知道如何解析位置相关信息。

非常感谢任何帮助。

谢谢。

2 个答案:

答案 0 :(得分:1)

假设您要保持位置字段嵌套......

       var xdoc = XDocument.Parse(xml);
       var events = from e in xdoc.Descendants("event")
                     select new {
                         Book = e.Element("book").Value,
                         Author = e.Element("author").Value,
                         BookImgUrl = e.Element("bookImgUrl").Value,
                         Info = e.Element("info").Value,
                         Date = e.Element("date").Value,
                         Time = e.Element("startTime").Value,
                         Location = new {
                             Name = e.Element("location").Element("name").Value,
                             Address = e.Element("location").Element("address").Value,
                             City = e.Element("location").Element("city").Value,
                             County = e.Element("location").Element("county").Value,
                             Postcode = e.Element("location").Element("postcode").Value,
                             Tel = e.Element("location").Element("tel").Value
                         }
                     };

            Console.WriteLine(events.First().Location.City);

答案 1 :(得分:1)

您必须决定是否使用(string)XElement广告投放或XElement.Value媒体资源。

(string)e.Element("book").Value编译并正常工作,但XElement.Value已经是一个字符串,因此将其转换为字符串是没有意义的。我建议使用(string)XElement,因为当找不到Element时,它不会导致NullReferenceException

您还可以使用let关键字只获取e.Element("location")一次,然后使用它来获取所有与Location相关的值:

var xdoc = XDocument.Parse(xml);
var events = from e in xdoc.Descendants("event")
             let l = e.Element("location")
             select new {
                 Book = (string)e.Element("book"),
                 Author = (string)e.Element("author"),
                 BookImgUrl = (string)e.Element("bookImgUrl"),
                 Info = (string)e.Element("info"),
                 Date = (string)e.Element("date"),
                 Time = (string)e.Element("startTime"),
                 Location = new {
                     Name = (string)l.Element("name"),
                     Address = (string)l.Element("address"),
                     City = (string)l.Element("city"),
                     County = (string)l.Element("county"),
                     Postcode = (string)l.Element("postcode"),
                     Tel = (string)l.Element("tel")
                 }
             };

Console.WriteLine(events.First().Location.City);