0

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.....

2
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by arjunpk
Featured Replies
  • 1

    You can traverse child nodes quite easily in C#. Example: [code] // 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") { … Read More

0

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....

1

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.

Votes + Comments
thank you.. :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.