我得到了这样的XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Some comment-->
<Databook>
<Note>
<Name>Camera2 made a snapshoot #243</Name>
<Value>Camera2_snapshoot-2013-09-06_21-47-35.png</Value>
</Note>
<Note>
<Name>Camera1 made a snapshoot #244</Name>
<Value>Camera1_snapshoot-2013-09-06_21-47-39.png</Value>
</Note>
</Databook>
我希望获得特定节点的字符串beetwen [Value] .. [/ Value],只知道它的字符串[Name] .. [/ Name]。
这是我到目前为止所做的:
string xmlfile = string.Format("XML/Diary/" + day);
XDocument dailyXML = XDocument.Load(xmlfile);
XElement Contact = (from xml2 in dailyXML.Descendants("Note")
where xml2.Element("Name").Value == item
select xml2).FirstOrDefault();
答案 0 :(得分:1)
如果你只想要Value
的值Name
等于item
尝试:
string result = (from xml2 in dailyXML.Descendants("Note")
where xml2.Element("Name").Value == item
select xml2.Element("Value").Value).FirstOrDefault();
或
string result = dailyXML.Descendants("Note")
.Where(n => n.Element("Name").Value == item)
.FirstOrDefault(n => n.Element("Value").Value);