Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the name of the root node of a given xml file

Tags:

c#

Im using c#.net windows form application. I have a xml file whose name is hello.xml and it goes like this

<?xml version="1.0" encoding="utf-8" ?> 
<languages>
  <language>
    <key>abc</key> 
    <value>hello how ru</value> 
  </language>
  <language>
    <key>def</key> 
    <value>i m fine</value> 
  </language>
  <language>
    <key>ghi</key> 
    <value>how abt u</value> 
  </language>
</languages>

How can i get the root node i.e <languages> into a text box. At this time I will have the xml file name. i.e "hello.xml". Using this I should get the root node.

like image 394
saeed Avatar asked May 05 '10 05:05

saeed


1 Answers

Using LINQ to XML you can do this:

XDocument doc = XDocument.Load("input.xml");
string rootLocalName = doc.Root.Name.LocalName;
textBox1.Text = '<' + rootLocalName + '>';

With XmlDocument you can use this:

XmlDocument doc = new XmlDocument();
doc.Load("input.xml");
string rootName = doc.SelectSingleNode("/*").Name;
like image 129
Mark Byers Avatar answered Sep 24 '22 04:09

Mark Byers