如果遇到值为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
if
<alarm>true</alarm>
遇到了。
答案 0 :(得分:2)
您可以使用以下查询来获取最后一个用户元素的日期和时间:
var lastUser = temp.Elements("User").LastOrDefault();
var date = (string)lastUser.Element("date");
var time = (string)lastUser.Element("time");
var isAlarm = (bool?)lastUser.Element("alarm");
此处更新是查询,它将获得所有用户的DateTime
值和警报值:
var users = from u in temp.Descendants("USER")
let time = (string)u.Element("time")
let date = (string)u.Element("date")
select new {
Date = DateTime.ParseExact(date + time, "MM.dd.yyyyHH:mm", null),
IsAlarm = (bool?)u.Element("alarm") ?? false
};
您将能够获取最后一位用户的日期,具有最大日期的用户,或按警报值过滤用户。例如。得到最后一次警报的时间:
var user = users.Where(u => u.IsAlarm).OrderBy(u => u.Date).LastOrDefault();
if (user != null)
date = user.Date;
答案 1 :(得分:2)
您可以使用以下代码获取最后一个节点:
var lastNode = doc.Root.Elements().Where(p => p.Name == "USER" && p.Elements().Any(o => o.Name == "alarm" && o.Value == "true")).Last();
答案 2 :(得分:1)
确定,
var yourUser = temp.Root.Elements("USER").LastOrDefault(u =>
u.Elements("alarm").Any(a => a.Value == true));
if (yourUser != null)
{
var dateTime = DateTime.ParseExact(
(string)yourUser.Element("date") + (string)yourUser.Element("time"),
"dd.MM.yyyyHH:mm");
}
答案 3 :(得分:0)
更直接的替代方法是使用更多的旧学校XPath,XPathSelectElements使用alarm = true
找到最后一个用户,如下所示:
var lastUser = xdoc.XPathSelectElements("//USER[alarm='true']")
.LastOrDefault();
var lastUserDate = lastUser.Element("date").Value;
var lastUserTime = lastUser.Element("time").Value;
AFAIK这或多或少等同于Linq:
var lastUser = xdoc.Descendants("USER")
.LastOrDefault(u => u.Element("alarm") != null
&& u.Element("alarm").Value == "true");
var lastUserDate = lastUser.Element("date").Value;
var lastUserTime = lastUser.Element("time").Value;
答案 4 :(得分:0)
使用XElement代替XDocument,以便循环xml内容的所有元素和属性
XElement temp = XElement.Load(xmlReader);
之后,就这样做:
foreach (XElement user in temp.Elements("USER"))
{
var alarm = user.Element("alarm");
if (alarm != null && alarm.Value == "true")
{
var date = user.Elements("date").Last().Value;
var time = user.Elements("time").Last().Value;
}
}