<Vehicle>
<CheckPoints ID="11">
<Days Day="1">check</Days>
</CheckPoints>
<CheckPoints ID="10">
<Days Day="1">check</Days>
</CheckPoints>
</Vehicle>
我希望使用LINQ查询记录。 我的问题是,我想迭代到每个节点并将CheckPoints Id与我的Id匹配,然后想要将Days文本提供到当天。 (每个CheckPoints将根据正在进行的月份具有最多31天节点)。 请帮忙。
答案 0 :(得分:0)
试试这个
var xmlstr = @"<Vehicle> <CheckPoints ID=""11""> <Days Day=""1"">check</Days> </CheckPoints> <CheckPoints ID=""11""> <Days Day=""2"">check</Days> </CheckPoints><CheckPoints ID=""10""> <Days Day=""1"">check</Days> </CheckPoints></Vehicle>";
var id=11;
var currDay = DateTime.Now.Day;
var xl = XElement.Load(new StringReader(xmlstr));
var resxml = from el in xl.Descendants("CheckPoints")
where Convert.ToInt32(el.Attribute("ID").Value) == id
select el into x
from ell in x.Descendants("Days")
where Convert.ToInt32(ell.Attribute("Day").Value) <= currDay
select ell;
foreach (var xres in resxml)
{
Console.Write(xres.Value);
}