0
我想从本地机器以相同的格式循环几个XML文件,但一些文件有缺少的元素,这是图像。当循环到达带有缺失元素的文件时,程序在选择新的时候崩溃。如何查询xml元素使用linq是否存在该元素C#
XML:
<?xml version="1.0" encoding="UTF-8"?>
<node>
<id>1663</vid>
<title>My Title</title>
<body>
<und keys="1">
<n0>
<value>123 Street</value>
<summary></summary>
<format type="NULL"></format>
<safe_value>123 Street</safe_value>
<safe_summary></safe_summary>
</n0>
</und>
</body>
<field keys="1"></field>
<image>
<und keys="1">
<i0>
<uri>https://uei.jpg</uri>
</i0>
<i1>
<uri>https://myurl.jpg</uri>
</i1>
</und>
</image>
</node>
LINQ的C#(WPF)APP:
XDocument document = XDocument.Load(file);
var nodes = from b in document.Descendants("node")
let imgs = b.Element("image")
select new
{
ID = (string)b.Element("id").Value,
Title = (string)b.Element("title").Value,
StreetName = (string)b.Element("body").Element("und").Element("n0").Element("value").Value,
Images = imgs.Descendants("und").ToList()
};