473,398 Members | 2,389 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,398 software developers and data experts.

Reading child nodes with XmlTextReader?

Hello,

how can I read values of child nodes with the XmlTextReader?

File format:
<root>
<items>
<item>
<field1>value1</field1>
<field2>value2</field2>
</item>
<item>
..
</item>
</items>
</root>

I'm looping with the XmlTextReader through the file:

while(Reader.Read())
{
switch(R.Name)
{
case "item":
// how can I get on this position the values of field1 or
field2?
}
}

The problem I see: If I request the field1 at the switch statement, I don't
longer know in which main node I am. Does anyone has a solution for this?
Maybe how to get the childnodes as XmlNode objects or something like this?

I don't want to use XmlDocument if possible because of performance reasons.
Thanks!

Emsi

Nov 12 '05 #1
1 16361
"Emsi" <mc@@ness.at> wrote in message news:%2****************@TK2MSFTNGP15.phx.gbl...
how can I read values of child nodes with the XmlTextReader? : : The problem I see: If I request the field1 at the switch statement, I don't
longer know in which main node I am. Does anyone has a solution for this?
The usual approach to this operates on the concept of detecting when you
enter element 'item' (LocalName = item and NodeType = Element), and
leave element 'item' (LocalName = item and NodeType = EndElement).
In implementing this typical solution, you have a range of choices in using
a flag, counter, or perhaps a Stack.

bool insideItem = false, insideField1 = false;
nmItem = R.NameTable.Add( "item");
nmField1 = R.NameTable.Add( "field1");
while( R.Read( ))
{
if ( R.LocalName == nmItem )
insideItem = ( R.NodeType != XmlNodeType.EndElement );
else if ( insideItem && R.LocalName == nmField1 )
insideField1 = ( R.NodeType != XmlNodeType.EndElement );
else if ( R.NodeType == XmlNodeType.Text && insideField1 )
DoSomething( nmField1, R.Value );
}

Slightly more elegant is to subclass an XmlTextReader, and then
override the ReadStartElement( ) and ReadEndElement( ) methods
to raise/lower the flag and capture the text node in an overload of
ReadString( ). Note that ReadString( ) is also called when reading
attribute values, so in general you may want to have a flag that'd
exclude attributes.

A subclass removes much of the complexity that can arise in writing
code that consumes the XmlTextReader (that complexity moves into
the subclass -- but it's more manageable there and it's services are
reusable from multiple points in your application without repeating
the same state transition logic.
Maybe how to get the childnodes as XmlNode objects or something like this?
I don't want to use XmlDocument if possible because of performance reasons.


You can use an empty XmlDocument here for the purpose of calling
ReadNode( ) on it, which will create an XmlNode from the 'item'
element's content.

Every XmlNode must have an XmlDocument that owns it, so this is
quite impossible without an XmlDocument. The inferior performance,
compared to XmlReaders, that appears to be behind your reasoning
here is primarily the overhead of creating XmlNodes.

An XmlReader can only tell you what it has read. Since you've only just
read the start element of 'item', there are no shortcuts to find out all child
nodes beneath it until you've read through to the end of element 'item'.
Derek Harmon
Nov 12 '05 #2

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

Similar topics

13
by: kaeli | last post by:
Can anyone explain this to me? It's driving me insane. Save this and run it in IE or Opera and then in Mozilla or Netscape 6+. In IE/Opera, I get the expected 4 alerts. In Mozilla/Netscape, I...
5
by: Liz | last post by:
C# .NET creating a tree programmatically treeView1.BeginUpdate(); treeView1.Nodes.Clear(); treeView1.Nodes.Add(new TreeNode("Client Details")); tvSelect.Nodes.Add(new TreeNode("Referrals"));...
2
by: Jack | last post by:
Hello, I am trying use a TreeView with checkboxes. I would like to check more than one node and allow all child nodes of selected nodes to be checked or unchecked with the parent is checked. ...
1
by: Daniel Rucareanu | last post by:
Hello, Does anybody knows how can you delete, in just one step, not using a loop, a subset of the child nodes of a given DOM parent node? The subset will be continous, so for example, if the...
14
by: marfi95 | last post by:
I'm trying to iterate through a list of child nodes. It seems like to get the text value of the node, you have to do a node->getFirstChild()->getNodeValue. This being said, there is a...
3
by: jismon | last post by:
Hi, I have one problem when i'm trying to loop thorugh the child nodes in a XML string in JAVA. The first Loop is working but in the child node there i want to loop and get the values but it doesn't...
2
by: GreggaR0und | last post by:
Hello; I'm trying to iterate through the sub nodes of a child node, but I'm getting a compile error on the nested For...Next expression saying it is invalid. Using the below code, the HasChild()...
2
by: arggg | last post by:
I am trying to add child nodes based on database query in which the users in the database are a part of the parent node. Here is my code. private void GetUsersByGroup() { ...
0
by: basheed abdal | last post by:
In the below code was executed succesful but it doesn't added child and subnodes plse send me code how can i add child nodes and subchild nodes in treeview aswellas in to database ,pleas help me ...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.