hi....
i have a small problem using the DOM parser for XML in c#

XmlDocument doc = new XmlDocument();
XmlNodeList _list = null;
doc.Load(location.Text);
_list = doc.GetElementsByTagName("DataSource");
foreach (XmlNode node in _list)
{
   //some processing

}

over here i am able to get specific nodes using the getElementByTagName method.....
after this is done.....
how do i browse the child nodes of the nodes i've already selected......

<DataSource>
  <DataSources>
    <Name>Source1</Name>
  </DataSources>
  <DataSources>
    <Name>Source2</Name>
    <Property>Hidden<Property>
  </DataSource>
</DataSource>

my code selects all the DataSource nodes in the xml.....
now for each of the DataSource nodes in the list that my code fetches....
i want to get browse the child nodes.... get the value of Name...
and if there is a Property field available, then i want to get the value of Property.....


Please help.....

Recommended Answers

All 3 Replies

the xml is just an example.....
the actual xml is 20 pages long.....
that is why i haven't put the original file into the question....

You can traverse child nodes quite easily in C#.
Example:

// assume here I have a DataSource node called node
foreach (XmlNode childNode in node.ChildNodes)
{
  string nodeValue = String.Empty;
  // use a case insensitive search on the node name
  string childNodeName = childNode.Name.ToLowerInvariant();
  if (childNodeName == "name")
  {
    nodeValue = childNode.Value;
    // do something meaningful with the value of the node...

  }
  else if (childNodeName == "property")
  {
    nodeValue = childNode.Value;
    // do some other meaningful thing with the value of the node...

  }
  else
  {
    // error - invalid node so do some error handling here...

  }
}

Hope this helps.

commented: thank you.. :) +1

darkagn....
definitely helps... :)
thanks a lot.... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.