在LINQ中解析xml元素及其属性 [英] Parse xml elements and their attributes in LINQ
问题描述
今天是我第一次接触LINQ,所以我是新手,这是一种轻描淡写的说法.因此,我要做的是解析STAT元素及其属性的值.我的样本XML看起来像这样.
Today is the first time I ever expose to LINQ so to say I am newbie is an understatement.. So what I am trying to do is to parse the value of the STAT elements and their attributes. My sample XML looks like this.
<ILS TGT="00-25-CE-94-00-05" PID="404001C5354144E4DA40E01F5000002" ORGCODE="00010019" xmlns="Mytest/v1.0">
<STAT TIME="000000018" TYPE="SYS" FIELD="PWR_V" VAL="1196"/>
<STAT TIME="000000018" TYPE="CAN" NID="65" FIELD="LAST_EC" VAL="EC_HEARTBEAT_TIMEOUT"/>
<STAT TIME="000000018" TYPE="NWK" FIELD="W_RSSI" VAL="-85"/>
<EVT TIME="0000000017" TYPE="ACC" SUBTYPE="GRANTED" CRDT="DPIN" CRED1="1212"/>
<EVT TIME="0000000018" TYPE="ACC" SUBTYPE="GRANTED" CRDT="DPIN" CRED1="1212" CRED2="2345"/>
<EVT TIME="0000000019" TYPE="ACC" SUBTYPE="DENIED" CRDT="OCRD" CRED1="0000DE0871"/>
<EVT TIME="0000000020" TYPE="ACC" SUBTYPE="GRANTED" CRDT="DCRD" CRED1="0000DE0871" CRED2="2345"/>
<EVT TIME="0000000021" TYPE="CFG" SUBTYPE="RELOCK_TIME" VAL="300"/>
<EVT TIME="0000000022" TYPE="LOG" SUBTYPE="HB_TIMEOUT" VAL="65"/>
</ILS>
因此,在此板上搜寻时,我发现了以下问题回答了我的问题.但是,我应用了该命令,由于某种原因,我根本无法获得任何结果,任何人都可以快速查看一下并指出我做错了什么吗?
So upon searching around on this board I found the following question which answers my question. However, I applied that and for some reason I can't get any result at all, can anybody take a quick look and point to what I have done wrong?
public static void ParseXML(string data)
{
try
{
XDocument xDoc = XDocument.Parse(data);
XElement root = xDoc.Root;
Console.WriteLine("TGT: " + root.Attribute("TGT").Value + "\n");
Console.WriteLine("PID: " + root.Attribute("PID").Value + "\n");
Console.WriteLine("ORGCODE: " + root.Attribute("ORGCODE").Value + "\n");
Console.WriteLine("xmlns: " + root.Attribute("xmlns").Value + "\n");
//Everything above this line is good, I was able to get the print out.
var eleSTAT = from node in xDoc.Descendants("STAT")
select new
{
attrTIME = node.Attribute("TIME").Value,
attrTYPE = node.Attribute("TYPE").Value,
attrFIELD = node.Attribute("FIELD").Value,
attrVAL = node.Attribute("VAL").Value,
};
//When I run my code, it never got into this foreach loop, aas if eleSTAT is empty
foreach (var s in eleSTAT)
{
Console.WriteLine("TIME: " + s.attrTIME + "\n");
Console.WriteLine("TYPE: " + s.attrTYPE + "\n");
Console.WriteLine("FIELD: " + s.attrFIELD + "\n");
Console.WriteLine("VAL: " + s.attrVAL + "\n");
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
我的最后一个问题,因为我有多个STAT元素,上面的方法行得通吗?我想让所有4个STAT元素及其属性返回给我.我认为这必须以某种类型的循环来完成?我说我的foreach循环应该已经这样做了吗?
And my last question, since I have multiple STAT element, would the above work? I want to have all 4 STAT elements and their attributes return to me. I figure this has to be done in some type of loop? am I correct in saying my foreach loop should already does this?
推荐答案
您必须将XML名称空间提供给后代.
You have to provide the XML namespace to Descendants.
像这样更改代码:
...
XNamespace xmlns = "Mytest/v1.0";
var eleSTAT = from node in xDoc.Descendants(xmlns + "STAT")
...
这篇关于在LINQ中解析xml元素及其属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!