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

问题描述:

我正在研究一个小的可执行应用程序。该应用程序只是一个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 
         } 
     } 
    } 
} 

我得到卡住,我到达位置节点,我不知道如何解析位置相关的信息。

任何帮助将不胜感激。

谢谢。

你必须决定是否你使用(string)XElement铸造或XElement.Value财产。

(string)e.Element("book").Value编译和工作正常,但XElement.Value已经是一个字符串,因此将其转换为字符串是毫无意义的。我建议使用(string)XElement,因为当元素不会被找到时它不会导致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); 

假设你想保持嵌套在位置字段...

 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); 
+0

当你使用'XElement.Value'属性转换为String是没有意义的.. 。 – MarcinJuraszek 2013-04-06 08:57:08

+0

@MarcinJuraszek谢谢;我从问题中复制了它。固定。 – 2013-04-06 11:05:42