如果遇到具有值的节点,我只想获取最后一个子节点date
并time
在 XML 文件中:alarm
true
XML 看起来是这样的:
<USER>
<date>09.07.2013</date>
<time>08:28</time>
<alarm>true</alarm>
</USER>
<USER>
<date>09.07.2013</date>
<time>08:23</time>
</USER>
<USER>
<date>09.07.2013</date>
<time>08:17</time>
<alarm>true</alarm>
</USER>
<USER>
<date>09.07.2013</date>
<time>08:15</time>
</USER>
我的代码是:
string URL = "http://IP";
HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
request.Credentials = new NetworkCredential("Username", "PW");
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
XmlReader xmlReader = XmlReader.Create(response.GetResponseStream());
XDocument temp = XDocument.Load(xmlReader);
var date = temp.Element("USER").Descendants("date").Select(node => node.Value.ToString()).First();
var time = temp.Element("USER").Descendants("time").Select(node => node.Value.ToString()).First();
}
正如我之前所说,我只想检索date
并且time
如果
<alarm>true</alarm>
遇到了。