Using XmlDocument.ImportNode() and XmlNodeList, innerXml issues

P

Peter Nofelt

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
<things>
<people>
<name>Peter</name>
<name>Lindsay</name>
</people>
<animals>
<name>dog</name>
<name>cat</name>
<name>bear</name>
</animals>
</things>


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();
l_xmlDocThings.Load("things.xml");

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

Thanks!
Pete
 
I

Ion Vasilian

Hi Peter,

Try the following snippet. It should do want you asked for:

XmlDocument l_xmlDocThings = new XmlDocument();
l_xmlDocThings.Load("things.xml");
XmlNodeList l_xmlNodePeople = l_xmlDocThings.SelectNodes("//people");
XmlDocument l_xmlDocPeople = new XmlDocument();
l_xmlDocPeople.AppendChild(l_xmlDocPeople.ImportNode(l_xmlNodePeople[0],
true));
XmlNodeList l_xmlNodeNames = l_xmlDocPeople.SelectNodes("//name");

Note that the 2nd parameter for ImportNode() is "true" since
you want to import the whole fragment rooted at "people" vs.
only the element itself. ImportNode() facilitates the construction
of nodes but does not attach them in the tree, hence the need
for the AppendChild(). Also, instead of GetElementsByTagName()
try using SelectNodes(). The later is significantly faster and much
more expressive wrt the queries that can be performed on the tree.
For example, you can skip a couple of steps and create the same
node list as above simply using one line of code:

XmlNodeList l_xmlNodeNames = l_xmlDocThings.SelectNodes("//people/name");

Ion

Peter Nofelt said:
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
<things>
<people>
<name>Peter</name>
<name>Lindsay</name>
</people>
<animals>
<name>dog</name>
<name>cat</name>
<name>bear</name>
</animals>
</things>


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();
l_xmlDocThings.Load("things.xml");

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

Thanks!
Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top