how to retrieve attributes using LINQ

Total Post:128

Points:898
XML 
XML
 1598  View(s)
Ratings:
Rate this:

I am trying to get ALL attributes from "server" below:

This is the XML file(partial):

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<server xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="udsServerInfo.xsd" id="57672acc-4ba7-4876-811a-1629eca853ed" productPath="C:\Tools\UDS\udsServerInfo\" osActivated="true">
  <networkAdapters>
Here is the code which returns NULL for "server":
    var server = (from node in doc.Attributes("server")
                       select node);

I am able to get all networkAdapters by calling doc.Descendants("networkAdapter") but haven't figured out how to obtain the attributes .

Thank you

  1. Post:145

    Points:1019
    Re: how to retrieve attributes using LINQ

    Hi Ashish,

    Try this (assuming doc is an XDocument):

    var attributes =doc.Descendants("server").Attributes();
    or
    var id =doc.Descendants("server").Attributes("id");
    or
    var productPath =doc.Descendants("server").Attributes("productPath");

      Modified On Apr-03-2018 06:20:41 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!