The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Using XmlDocument.ImportNode() and XmlNodeList

Hey All,

I’m running into this issue with parsing through an xml document by tag name. Below is an example xml document:

File Name: things.xml

As you can see, this xml structure has multiple sublevels. I wish to parse my xml by tag name (example: get elements by tag name “people”, then within here, get elements by tag name “name”). I am trying to access these sublevels via the XmlDocument class in conjunction with the XmlNodeList class. Note the C# code snippet below:

//Create XmlDocument object and load file
XmlDocument l_xmlDocThings = new XmlDocument();

// get node elements based on tag name "people". WORKS
XmlNodeList l_xmlNodePeople = l_xmlDocThings.GetElementsByTagName("people");

// create new xmlDocument and assign it to list of people
XmlDocument l_xmlDocPeople = new XmlDocument();
l_xmlDocPeople.ImportNode(l_xmlNodePeople[0], false);
// Unable to get elements based on tag name "name" because
// innerXML of l_xmlDocPeople is ""
XmlNodeList l_xmlNodeNames = l_xmlDocPeople.GetElementsByTagName("name");

When I retrieve the elements “people”, this works. But when I create a new XmlDocument based on the “people” XmlNodeList[0], I see that I run into issues. The innerXml attribute of l_xmlNodePeople[0] is not imported into l_xmlDocPeople.

What am I doing wrong?

Note: I just wish to access the node structure by tag-name. If you feel there is a better approach to getting elements by tag name, please let me know.

Pete Send private email
Wednesday, May 04, 2005
Have you tried using XPath? If I understand what you are trying to accomplish, XPath seems to be what you need.  Check out:
Thursday, May 05, 2005
P.S.  Use the SelectNodes (or SelectSingleNode) method on the XMLDocument to evaluate the XPath expression in the context of the document.  I should have been clearer before.
Thursday, May 05, 2005

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz