Example of how to parse a XML document in C# with DOM

Example of how to parse a XML document in C# with DOM

Post by Mason Ho » Wed, 18 Dec 2002 17:46:23



<CustomersDataSet>
        <Customers>
                <ContactName>Yvonne Moncada</ContactName>
                <City>Buenos Aires</City>
                <Country>Argentina</Country>
                <OrderId>10409</OrderId>
                <OrderDate>1997-01-09T00:00:00.0000000-06:00</OrderDate>
                <ProductId>14</ProductId>
        </Customers>
        <Customers>
                <ContactName>Yvonne Moncada</ContactName>
                <City>Buenos Aires</City>
                <Country>Argentina</Country>
                <OrderId>10409</OrderId>
                <OrderDate>1997-01-09T00:00:00.0000000-06:00</OrderDate>
                <ProductId>21</ProductId>
        </Customers>
        <Customers>
                <ContactName>Sergio Gutirrez</ContactName>
                <City>Buenos Aires</City>
                <Country>Argentina</Country>
                <OrderId>10448</OrderId>
                <OrderDate>1997-02-17T00:00:00.0000000-06:00</OrderDate>
                <ProductId>26</ProductId>
        </Customers>
</CustomersDataSet>

//myDS is a Dataset.

string sXML = myDS.GetXml();

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.LoadXml(sXML);

// if you have a raw XML file, just substitute next call with Load
//xmlDoc.Load(Server.MapPath("xmlDocument.xml"));

// just to retrieve the first child, you can omit next 2 lines..
XmlNode root = xmlDoc.FirstChild;
string s = root.FirstChild.OuterXml;
// just to retrieve the first child

// just for debug
s = xmlDoc.InnerXml.ToString();

// this is where it all happens
XmlNodeList myNodeList = xmlDoc.SelectNodes("/CustomersDataSet/Customers");

for (int on0Nodes = 0; on0Nodes < myNodeList.Count; on0Nodes++)
{
        //Get each child of on1 nodes and tear it up.
        XmlNodeList on1NodeList = myNodeList.Item(on0Nodes).ChildNodes ;
        for (int on1Nodes = 0; on1Nodes < on1NodeList.Count; on1Nodes++)
        {
                //Show sample of Attribute
                XmlNode att = on1NodeList.Item(on1Nodes);
                //print the att value
                if (att != null)
                {
                        Console.WriteLine(att.InnerText);
                }
        }

Quote:}

Hope it helps someone!

Mason.

 
 
 

Example of how to parse a XML document in C# with DOM

Post by Bachlechner Al » Fri, 20 Dec 2002 01:37:31


[...]

Quote:> XmlNodeList myNodeList = xmlDoc.SelectNodes("/CustomersDataSet/Customers");

[...]
Nice Help. But what can I do if the nodes aren't known by me. For
example if I only want to get an output of all elements and all nodes,
no matter what kind of xml it is and I don't have any knowledge about
the structure of the xml-File.

Quote:

> Hope it helps someone!

> Mason.

thx
a-x-i

 
 
 

Example of how to parse a XML document in C# with DOM

Post by Mason Ho » Wed, 01 Jan 2003 04:27:29


Hi Alex;

I would look at this site :

http://www.xmlforasp.net/CodeBank/System.Xml.Schema/BuildSchema/Build...

This example shows how you can create a schema based on an XML file.
Once you have a schema, your avenue is wide open..

Thx.
Mason.



> [...]
> > XmlNodeList myNodeList = xmlDoc.SelectNodes("/CustomersDataSet/Customers");

> [...]
> Nice Help. But what can I do if the nodes aren't known by me. For
> example if I only want to get an output of all elements and all nodes,
> no matter what kind of xml it is and I don't have any knowledge about
> the structure of the xml-File.

> > Hope it helps someone!

> > Mason.

> thx
> a-x-i