tags:

views:

2236

answers:

5

Duplicate: This is a duplicate of Best practices to parse xml files with C#? and many others (see http://stackoverflow.com/search?q=c%23+parse+xml). Please close it and do not answer.


How do you parse XML document from bottom up in C#?

For Example :

<Employee>
   <Name> Test </name>
   <ID> 123 </ID>
<Employee>
<Company>
    <Name>ABC</company>
    <Email>[email protected]</Email>
 </company>

Like these there are many nodes..I need to start parsing from bottom up like..first parse <company> and then and so on..How doi go about this in C# ?

+2  A: 

hi

checkout the System.Xml Namespace

http://msdn.microsoft.com/en-us/library/system.xml%28VS.80%29.aspx

nWorx
A: 

Use XML serialization

Thomas Levesque
A: 

You can use the XmlSerializer class. It takes your class and can read/write it to XML files really easily.

speps
+1  A: 

Try this:

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\Path\To\Xml\File.xml");

Or alternatively if you have the XML in a string use the LoadXml method.

Once you have it loaded, you can use SelectNodes and SelectSingleNode to query specific values, for example:

XmlNode node = doc.SelectSingleNode("//Company/Email/text()");
// node.Value contains "[email protected]"

Finally, note that your XML is invalid as it doesn't contain a single root node. It must be something like this:

<Data>
    <Employee>
        <Name>Test</name>
        <ID>123</ID>
    </Employee>
    <Company>
        <Name>ABC</company>
        <Email>[email protected]</Email>
    </Company>
</Data>
Jon Grant
@Jon: you _do_ know this is about to be closed as a duplicate, right?
John Saunders
+2  A: 

I like Linq's XDocument.

DanDan