I am need to get attribute from node.
Sometimes I get different attribute, e.g sometimes it is <attribute id="0x1162834"> and sometimes <attribute-list id="0x1162834">, and I don't know how to get the attribute
Thanks for any help.
This is my 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> This is my code in 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; } } }