how to retrieve attributes using LINQ

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

Last updated:12/10/2013 12:07:59 AM

1 Answers

Anonymous User
Anonymous User

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

Answer