开发者

Retreving Data from XML using LINQ

开发者 https://www.devze.com 2023-01-23 03:20 出处:网络
<Vehicle> <CheckPoints ID=\"11\"> <Days Day=\"1\">check</Days> </CheckPoints>
<Vehicle>
  <CheckPoints ID="11">
    <Days Day="1">check</Days>
  </CheckPoints>
  <CheckPoints ID="10">
    <Days Day="1">check</Days>
  </CheckPoints>
</Vehicle>

and i want to query record using LINQ. My question is, i want to iterate to each node and Match the CheckPoints Id with my Id and then want to get the Days text up to the current day. (Each CheckPoints will have Max 31开发者_开发问答 Days node as according to the Month which is going on). Please help.


Try this

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消