MatthewHagemann
MatthewHagemann

Reputation: 1195

How to read XML header information using Linq to XML

I am trying to figure out how to use Linq to XML to read an XML file into my C# program. Here is the example for my question:

<node name="services" class="tridium.containers.ServiceContainer" module="coreRuntime" release="2.301.532.v1">

How do I access the name, class, module, and release information in this line? I tried .element("node").Name for the name field, but that just returns "node". All of the tutorials I can find are either too simplistic to deal with this, or deal with writing an XML file. Please help.

Upvotes: 0

Views: 1075

Answers (2)

Steve
Steve

Reputation: 4523

If it is at the root, then

XDocument xdoc = XDocument.Load("data.xml");

var name= xdoc.Root.Attribute("name").Value;
var class= xdoc.Root.Attribute("class").Value;
var module= xdoc.Root.Attribute("module").Value;

Upvotes: 0

Perfect28
Perfect28

Reputation: 11317

You can use this :

XElement rootelement = XElement.Load(@"path_to_your_file") ;

var name = rootElement.Attribute("name").Value ; 
var classname = rootElement.Attribute("class").Value ; 
var module = rootElement.Attribute("module").Value ; 

Upvotes: 3

Related Questions