How to find the name of the root node of a given xml file
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>
<开发者_JAVA百科/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.
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;
Or use the XmlDocument DocumentElement
property as shown here:
XmlDocument doc = new XmlDocument();
doc.Load("hello.xml");
string root = doc.DocumentElement.Name;
textBox1.Text = "<" + root + ">";
精彩评论