我需要从节点获取属性。
有时我得到不同的属性,例如,有时是<attribute id="0x1162834">
和有时<attribute-list id="0x1162834">
,并且我不知道如何获得该属性
谢谢你的帮助。
这是我的XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model-response-list
xmlns="http://www.ca.com/spectrum/restful/schema/response" total-models="6119" throttle="1000">
<model-responses>
<model mh="0x504067">
<attribute id="0x12d7f">XX.XXX.XX.X</attribute>
<attribute id="0x11ee8">2</attribute>
<attribute id="0x118b9"></attribute>
<attribute error="NoSuchAttribute" id="0x1162834"/>
<attribute error="NoSuchAttribute" id="0x1161461"/>
</model>
<model mh="0x40007f">
<attribute id="0x12d7f">XX.XX.XX.X</attribute>
<attribute id="0x11ee8">9</attribute>
<attribute-list id="0x1162834">
<instance oid="0" value=" Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz "/>
<instance oid="1" value=" Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz "/>
</attribute-list>
<attribute-list id="0x1161461">
<instance oid="0" value="6"/>
<instance oid="1" value="6"/>
</attribute-list>
</model>
</model-responses>
<link type="application/xml" href="http://spectrum/spectrum/restful/devices/?id=53c271cb-cb69-4b13-b95f-50e39ebecd5e&start=1000&throttlesize=1000" rel="next"/>
</model-response-list>
这是我在C#中的代码:
using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
{
var XMLResponse = sr.ReadToEnd();
XDocument xmlDoc = XDocument.Parse(XMLResponse);
XName qualifiedName = XName.Get("model", "http://www.ca.com/spectrum/restful/schema/response");
foreach (var device_handel in xmlDoc.Descendants(qualifiedName))
{
XName QN_attr = XName.Get("attribute", "http://www.ca.com/spectrum/restful/schema/response");
attr_devices[0] = device_handel.Descendants(QN_attr).Where(e => e.Attribute("id").Value == "0x12d7f").Select(e => e).Single().Value;
attr_devices[1] = device_handel.Descendants(QN_attr).Where(e => e.Attribute("id").Value == "0x11ee8").Select(e => e).Single().Value;
attr_devices[2] = device_handel.Descendants(QN_attr).Where(e => e.Attribute("id").Value == "0x118b9").Select(e => e).Single().Value;
attr_devices[3] = device_handel.Descendants(QN_attr).Where(e => e.Attribute("id").Value == "0x1162834").Select(e => e).Single().Value;
attr_devices[4] = device_handel.Descendants(QN_attr).Where(e => e.Attribute("id").Value == "0x1161461").Select(e => e).Single().Value;
}
}
}
我们可以从这样的事情开始:
XName qualifiedName = XName.Get("model", "http://www.ca.com/spectrum/restful/schema/response");
XName QN_attr = XName.Get("attribute", "http://www.ca.com/spectrum/restful/schema/response");
XName QN_attrList = XName.Get("attribute-list", "http://www.ca.com/spectrum/restful/schema/response");
foreach (var device_handel in xmlDoc.Descendants(qualifiedName))
{
var attrsAndLists = device_handel.Elements().Where(x => x.Name == QN_attr || x.Name == QN_attrList).ToArray();
attr_devices[0] = attrsAndLists.Where(e => e.Attribute("id").Value == "0x12d7f").Select(e => e.Value).SingleOrDefault();
attr_devices[1] = attrsAndLists.Where(e => e.Attribute("id").Value == "0x11ee8").Select(e => e.Value).SingleOrDefault();
attr_devices[2] = attrsAndLists.Where(e => e.Attribute("id").Value == "0x118b9").Select(e => e.Value).SingleOrDefault();
attr_devices[3] = attrsAndLists.Where(e => e.Attribute("id").Value == "0x1162834").Select(e => e.Value).SingleOrDefault();
attr_devices[4] = attrsAndLists.Where(e => e.Attribute("id").Value == "0x1161461").Select(e => e.Value).SingleOrDefault();
Console.WriteLine(string.Join(", ", attr_devices));
}
请注意,attribute-list
它与的结构不同attribute
。尽管第二个元素包含一个包含“值”的文本元素,但不清楚您要从第一个元素中提取什么。嗯,在您的xml中,id="0x118b9"
仅在第一个模型中存在,而在第二个模型中不存在。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句