473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 17 '05 #1
1 12408
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 17 '05 #2

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

Similar topics

0
by: John Spiegel | last post by:
Hi all, I'm trying to efficiently pull data out of an xml file into a XmlDocument AND create another "sub" document based on one subtree of the document. For example, say I've got: <Books>...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.