[英]read XML using LINQ
I am newbie to LINQ. 我是LINQ的新手。 I am trying to read the following node and element values from XML using LINQ. 我正在尝试使用LINQ从XML读取以下节点和元素值。
DATA -- msgid and msgtime PTP -- percentage CONT1 -- get "url" value if the "type" = "RIGHT" 数据-msgid和msgtime PTP-百分比CONT1-如果“ type” =“ RIGHT”,则获取“ url”值
Please let me know. 请告诉我。
<DATA msgid="02123" msgtime="2008-02-29 15:30:02.123">
<PTP number="67" pert="READ" percentage="95" pertime="2008-02-29 15:30:02.123">
<Images>
<Image view="w1" type="IMAGE" percentage="85" distance="0" url="00002_tyd.jpg" />
</Images>
</PTP>
<CHAS1 sequence="1" number="58019" percentage="95" pertime="2008-02-29 15:30:02.123">
<Images>
<Image view="c1" type="WRONG" percentage="85" url="00002_ssj.jpg" />
<Image view="c2" type="RIGHT" percentage="85" url="00003_ssj.jpg" />
</Images>
<CONT1 number="58011" percentage="95" pertime="2008-02-29 15:30:02.123">
<Images>
<Image view="c1" type="WRONG" percentage="85" url="00002_csj.jpg" />
<Image view="c2" type="RIGHT" percentage="85" url="00003_csj.jpg" />
</Images>
</CONT1>
</CHAS1>
</DATA>
I would suggest you take a look at this: 我建议您看一下:
http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx
Particularly the part beginning with "Once these files are loaded into the LINQ to XML API, you can write queries over that tree." 特别是从“一旦将这些文件加载到LINQ to XML API中,您就可以在该树上编写查询了”。
Based on that, something like this should work for you: 基于此,这样的事情应该为您工作:
XDocument loaded = XDocument.Load(@"C:\data.xml");
var q = from c in loaded.Descendants("DATA")
select new
{
MsgId = (int)c.Attribute("msgid"),
MsgTime = (DateTime)c.Attribute("msgtime"),
PtpPercentage = (int)c.Element("PTP").Attribute("percentage")
ContUrls = from i in c.Element("CHAS1")
.Element("CONT1")
.Descendants("Image")
where (string)i.Attribute("type") == "RIGHT"
select (string)i.Attribute("url");
};
Not tested, but that should put you on the right track. 未经测试,但这应该使您走上正确的道路。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.