解析XML文件第二嵌套级别中的元素
我正在开发一个小的可执行应用程序。该应用程序只是一个XML解析器,它将解析XML文件并将解析后的数据存储到数据库中。因此,此应用程序将使用的XML文件具有以下结构:解析XML文件第二嵌套级别中的元素,xml,c#-4.0,xml-parsing,linq-to-xml,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<
<?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>
我在到达位置节点的地方卡住了,我不知道如何解析与位置相关的信息。假设您希望保持位置字段嵌套
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);
您必须决定是使用
(字符串)XElement
强制转换还是XElement.Value
属性
(string)e.Element(“book”).Value可以很好地编译和工作,但是XElement.Value
已经是一个字符串,所以将它转换为字符串是没有意义的。我建议使用(string)XElement
,因为当找不到元素时,它不会导致NullReferenceException
您还可以使用let
关键字仅获取一次e.Element(“位置”)
,然后使用它获取所有与位置相关的值:
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")
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);