1

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&amp;start=1000&amp;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;
        }
    }
}
0

1 Answer 1

1

We could begin with something like this:

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

Note that attribute-list has a different structure from attribute. While the second has a text element containing the "value", it isn't clear what you want to extract from the first. Ah and in your xml, the id="0x118b9" is present only in the first model, and not in the second model.

To extract the value of attribute-list do something like:

XName QN_instance = XName.Get("instance", "http://www.ca.com/spectrum/restful/schema/response");

...

attr_devices[0] = attrsAndLists.Where(e => (string)e.Attribute("id") == "0x12d7f").Select(e => e.Name == QN_attr ? e.Value : e.Elements(QN_instance).Select(f => (string)f.Attribute("value")).FirstOrDefault()).SingleOrDefault();

(note that I've removed all the various .Value and replaced them with (string) cast. It is a little better, because in case of missing attribute it won't throw an exception.

And you should make a method and put the entire attrsAndLists.Where(e => (string)e.Attribute("id") == "0x12d7f").Select(e => e.Name == QN_attr ? e.Value : e.Elements(QN_instance).Select(f => (string)f.Attribute("value")).FirstOrDefault()).SingleOrDefault() inside, instead of repeating it five times.

Sign up to request clarification or add additional context in comments.

1 Comment

how to get the attribute("value") of the first node from the list?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.