C# .NET - How to loop through child nodes and get the values from XML

Asked By guna guna on 04-Feb-10 12:34 AM
Hi,

   I have XML structure lik this,
   BODY{font:x-small 'Verdana';margin-right:1.5em} .c{cursor:hand} .b{color:red;font-family:'Courier New';font-weight:bold;text-decoration:none} .e{margin-left:1em;text-indent:-1em;margin-right:1em} .k{margin-left:1em;text-indent:-1em;margin-right:1em} .t{color:#990000} .xt{color:#990099} .ns{color:red} .dt{color:green} .m{color:blue} .tx{font-weight:bold} .db{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;border-left:1px solid #CCCCCC;font:small Courier} .di{font:small Courier} .d{color:blue} .pi{color:blue} .cb{text-indent:0px;margin-left:1em;margin-top:0px;margin-bottom:0px;padding-left:.3em;font:small Courier;color:#888888} .ci{font:small Courier;color:#888888} PRE{margin:0px;display:inline}
<?xml version="1.0" encoding="UTF-8" ?>
- <Hashtable xmlns="http://localhost/Schemas/HashtableSchema.xsd">
- <row name="channnelbest">
- <Hashtable xmlns="http://localhost/Schemas/HashtableSchema.xsd">
- <row name="ChannnelTop">
- <Hashtable xmlns="http://localhost/Schemas/HashtableSchema.xsd">
  <row name="com">available</row>
  </Hashtable>
  </row>
- <row name="BestTopLimos">
- <Hashtable xmlns="http://localhost/Schemas/HashtableSchema.xsd">
  <row name="com">available</row>
  </Hashtable>
  </row>
- <row name="BestTopTwenty">
- <Hashtable xmlns="http://localhost/Schemas/HashtableSchema.xsd">
  <row name="com">available</row>
  </Hashtable>
  </row>
- <row name="channnelbest.com">
- <Hashtable xmlns="http://localhost/Schemas/HashtableSchema.xsd">
  <row name="status">available</row>
  <row name="classkey">domcno</row>
  </Hashtable>
  </row>
  </Hashtable>
  </row>
  </Hashtable>


I need name value which is 'Channelbest','ChannelTop','BestTopLimos','BestTopTwenty' and 'Channelbest.com'.How to get those things.Can u help me plz..
Santhosh N replied to guna guna on 04-Feb-10 12:44 AM

you could actually use xpath to read the nodes and get the collection of names as you desired..

XmlNode root = doc.DocumentElement;
nodeList = root.SelectNodes("descendant::row[name='channnelbest']");

this gives the nodes matching the first condition as you want to get channelbest rows...

you can check http://msdn.microsoft.com/en-us/library/d271ytdx(VS.80).aspx for more info on this..


Sakshi a replied to guna guna on 04-Feb-10 12:45 AM

http://bytes.com/topic/c-sharp/answers/865325-c-reading-xml-node-quickest-way

http://www.dreamincode.net/code/snippet1685.htm


Huggy Bear replied to guna guna on 04-Feb-10 12:47 AM
First of all the xml which you have is not a valid one I suppose, because of the inclusion of the Html tags and styles. That has to be ripped of from the Xml file, then make use of the XmlDocument class as shown below,

public void GetNameAttributes()
{
    XmlDocument document = new XmlDocument();
    document.Load(@"D:\xmlfile.xml");

    //Gets all the tags with tag name row
    XmlNodeList nodeList = document.GetElementsByTagName("row");

    List<string> nameList = new List<string>();

    //Loop through each and every node
    foreach (XmlNode node in nodeList)
    {
        //Add the elements to the list
        nameList.Add(node.Attributes["name"].Value);
    }
    
    //now nameList has your required values.
}
How to loop through child nodes and get the values from XML - guna guna replied to Huggy Bear on 04-Feb-10 01:13 AM
Hi Huggy Bear,

  Your code is working fine..Thanks fro ur reply.
Thank You - Huggy Bear replied to guna guna on 04-Feb-10 02:10 AM
end of post