我正在开发一个小的可执行应用程序。该应用程序只是一个 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
}
}
}
}
我在到达 Location 节点的地方卡住了,我不知道如何解析与位置相关的信息。