472,805 Members | 863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Using XmlDocument.ImportNode() and XmlNodeList, innerXml issues

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

Nov 12 '05 #1
1 9567
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.ImportNo de(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" <pc******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hi, Is there an easier way to pull a subset of nodes from one XmlDocument to another? I have the code below but would like to know if there is a more streamlined method. Thanks, Dave ...
1
by: Peter Nofelt | last post by:
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>
0
by: Pierre | last post by:
Hi, I'm trying to select specific nodes from a XmlDocument filled with a serialized object and to insert these nodes into another XmlDocument. The object is well serialized (see below). From a...
11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
4
by: Carlos Albert | last post by:
Hello, Would you tell me if there is a way to extract a single node as a new xmldocument? Thanks.
1
by: GCeaser | last post by:
All, I have a very large XML document that contains two types of elements. I want to select all the elements of each type and place them in separate XML documents. This is what I have so...
2
by: Martin Pöpping | last post by:
Hello, I want to add some elements of one XmlDocument in another. My code looks like this: XmlDocument docA= new XmlDocument(); docA.LoadXml(record); XmlNodeList nl =...
6
by: Derek Hart | last post by:
I bring in an xml file into vb.net by using xmlDoc.LoadXml(XMLString) - I run xpath statements against the xml file to grab data from it, so I use, as an example, //Vehicles/Vehicles/@make to get...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.