无法从 XML 获取单个 XElement
Cannot get single XElement from XML
我有一个 XML 文件,其结构如下:
<scan client="Computer1" end="9/20/2016 7:00:00 AM" start="9/20/2016 7:00:00 AM">
<childfile>
<name>file.ext</name>
<lastmodified>8/31/2016</lastmodified>
<age>19</age>
</childfile>
<childfile>
<name>file2.ext</name>
<lastmodified>9/1/2016</lastmodified>
<age>18</age>
</childfile>
<childfile>
<name>file3.ext</name>
<lastmodified>8/19/2016</lastmodified>
<age>31</age>
</childfile>
<childfile>
<name>file4.ext</name>
<lastmodified>8/23/2016</lastmodified>
<age>27</age>
</childfile>
</scan>
<scan client="Computer2" end="9/20/2016 7:00:00 AM" start="9/20/2016 7:00:00 AM">
<childfile>
<name>file.ext</name>
<lastmodified>8/31/2016</lastmodified>
<age>19</age>
</childfile>
<childfile>
<name>file2.ext</name>
<lastmodified>9/1/2016</lastmodified>
<age>18</age>
</childfile>
<childfile>
<name>file3.ext</name>
<lastmodified>8/19/2016</lastmodified>
<age>31</age>
</childfile>
<childfile>
<name>file4.ext</name>
<lastmodified>8/23/2016</lastmodified>
<age>27</age>
</childfile>
</scan>
我正在发送一个新的 XML 元素,它看起来像上面的元素,它类似于:
<scan client="Computer1" end="9/25/2016 7:00:00 AM" start="9/25/2016 7:00:00 AM">
<childfile>
<name>file.ext</name>
<lastmodified>8/31/2016</lastmodified>
<age>19</age>
</childfile>
<childfile>
<name>file2.ext</name>
<lastmodified>9/1/2016</lastmodified>
<age>18</age>
</childfile>
</scan>
我如何搜索原始 XML 以查看其扫描部分的客户端属性是否与所提供的匹配,如果确实匹配,则将该元素替换为该元素假如。如果找不到匹配项,则只需将该元素添加到现有元素即可。
我尝试使用:
originalXML.Elements("scan").SingleOrDefault(e => e.Attribute("client").Value == client)
使用
设置客户端变量
string client = replacementXML.Attribute("client").Value;
似乎每次都返回 null,即使我检查了客户端字符串并将其设置为 "Computer1"。
想知道为什么总是返回 null 吗?
你有两个问题...
- 格式错误XML - 您必须有一个根元素
- 您需要从根对象中引用 "scan" 元素
因此您的 linq2xml 将如下所示...
var client = replacementXML.Root.Attribute("client").Value;
var match = originalXML.Root.Elements("scan")
.SingleOrDefault(e => e.Attribute("client").Value == client);
好吧,我想我有点晚了。我看到你的评论说你已经修好了。 :)
我有一个 XML 文件,其结构如下:
<scan client="Computer1" end="9/20/2016 7:00:00 AM" start="9/20/2016 7:00:00 AM">
<childfile>
<name>file.ext</name>
<lastmodified>8/31/2016</lastmodified>
<age>19</age>
</childfile>
<childfile>
<name>file2.ext</name>
<lastmodified>9/1/2016</lastmodified>
<age>18</age>
</childfile>
<childfile>
<name>file3.ext</name>
<lastmodified>8/19/2016</lastmodified>
<age>31</age>
</childfile>
<childfile>
<name>file4.ext</name>
<lastmodified>8/23/2016</lastmodified>
<age>27</age>
</childfile>
</scan>
<scan client="Computer2" end="9/20/2016 7:00:00 AM" start="9/20/2016 7:00:00 AM">
<childfile>
<name>file.ext</name>
<lastmodified>8/31/2016</lastmodified>
<age>19</age>
</childfile>
<childfile>
<name>file2.ext</name>
<lastmodified>9/1/2016</lastmodified>
<age>18</age>
</childfile>
<childfile>
<name>file3.ext</name>
<lastmodified>8/19/2016</lastmodified>
<age>31</age>
</childfile>
<childfile>
<name>file4.ext</name>
<lastmodified>8/23/2016</lastmodified>
<age>27</age>
</childfile>
</scan>
我正在发送一个新的 XML 元素,它看起来像上面的元素,它类似于:
<scan client="Computer1" end="9/25/2016 7:00:00 AM" start="9/25/2016 7:00:00 AM">
<childfile>
<name>file.ext</name>
<lastmodified>8/31/2016</lastmodified>
<age>19</age>
</childfile>
<childfile>
<name>file2.ext</name>
<lastmodified>9/1/2016</lastmodified>
<age>18</age>
</childfile>
</scan>
我如何搜索原始 XML 以查看其扫描部分的客户端属性是否与所提供的匹配,如果确实匹配,则将该元素替换为该元素假如。如果找不到匹配项,则只需将该元素添加到现有元素即可。
我尝试使用:
originalXML.Elements("scan").SingleOrDefault(e => e.Attribute("client").Value == client)
使用
设置客户端变量string client = replacementXML.Attribute("client").Value;
似乎每次都返回 null,即使我检查了客户端字符串并将其设置为 "Computer1"。
想知道为什么总是返回 null 吗?
你有两个问题...
- 格式错误XML - 您必须有一个根元素
- 您需要从根对象中引用 "scan" 元素
因此您的 linq2xml 将如下所示...
var client = replacementXML.Root.Attribute("client").Value;
var match = originalXML.Root.Elements("scan")
.SingleOrDefault(e => e.Attribute("client").Value == client);
好吧,我想我有点晚了。我看到你的评论说你已经修好了。 :)