score:0

Accepted answer

This may help you. Everything you wrote that code was right, you just rather use main.Element("Network").Attributes() than main.Attributes()

IEnumerable<XAttribute> successAttributes =
                 from attribute in main.Element("Network").Attributes()
                 where attribute.Name.LocalName == "Nid"
                 select attribute;

On behalf of this issue I wrote the below sample program which will yield the expected NID value

string strVal = "<Server><Network Nid=\"43d5377-0dcd-40e6-b95c-8ee980b1e248\"/><Client_Group id=\"20\">963440d0-96dc-46a4-a54d-7251a65f585f</Client_Group><ClientID id=\"20\">3fc8ffa1-c16b-4d7b-9e55-1e88dfe15277</ClientID></Server>";

            XElement main = XElement.Load(new StringReader(strVal));

            IEnumerable<XAttribute> successAttributes =
                 from attribute in main.Element("Network").Attributes()
                 where attribute.Name.LocalName == "Nid"
                 select attribute;

score:0

main here is the root element: <Server> - which does not have a Nid attribute.

You want something like:

Guid nid = (Guid)main.Element("Network").Attribute("Nid");

Or for multiple:

Guid[] arr = (from attr in main.DescendentsOrSelf().Attributes("Nid")
              select (Guid)attr).ToArray();

Related Articles