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

XmlTextReader - how find a particular node

I am reading an xml file from a URL. I was originally doing this using the
XmlDocument class. But this was very slow. The XmlTextReader is meant to be
much quicker for forward only retrieval of data.

I need to somehow access a particular list of nodes. e.g. I want to loop
through all the nodes called <item></item>. How is this possible using
XmlTextReader?
I can see that you can get the .Value property of XmlTextReader. But I want
to be able to specify the node I want to look at.

e.g.

<Group>
<Items>
<Item>content here</Item>
<Item>some more content here</Item>
<Items>
<Activity></Activity>
<Location></Location>
</Group>

thanks.

CR
Mar 13 '06 #1
4 44923
you can use ReadToFollowing("Item") to jump to each Item - however, you need
to be careful, as the "intuitive" code might be (IIRC) wrong:

while(reader.ReadToFollowing("Item")) {
string content = reader.ReadElementContentAsString();
// do something with content
}

The reason this is wrong is that ReadElementContentAsString progresses
/past/ the [Item] element, so the next call to ReadToFollowing() might skip
the next row; you might be able-to do something with a sub-reader, else you
might need to check (after calling ReadElementContentAsString) if you are on
an Item element; if you are, it is the *next* one, so read it; if you
aren't, then ReadToFollowing

Marc

"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:C1**********************************@microsof t.com...
I am reading an xml file from a URL. I was originally doing this using the
XmlDocument class. But this was very slow. The XmlTextReader is meant to
be
much quicker for forward only retrieval of data.

I need to somehow access a particular list of nodes. e.g. I want to loop
through all the nodes called <item></item>. How is this possible using
XmlTextReader?
I can see that you can get the .Value property of XmlTextReader. But I
want
to be able to specify the node I want to look at.

e.g.

<Group>
<Items>
<Item>content here</Item>
<Item>some more content here</Item>
<Items>
<Activity></Activity>
<Location></Location>
</Group>

thanks.

CR

Mar 13 '06 #2
Hello CodeRazor,

http://msdn.microsoft.com/library/de...snametopic.asp

use .Read() methods of XmlTextReader and then in switch (reader.NodeType)
check the case XmlNodeType.Element u want to find
C> I am reading an xml file from a URL. I was originally doing this
C> using the XmlDocument class. But this was very slow. The
C> XmlTextReader is meant to be much quicker for forward only retrieval
C> of data.
C>
C> I need to somehow access a particular list of nodes. e.g. I want to
C> loop
C> through all the nodes called <item></item>. How is this possible
C> using
C> XmlTextReader?
C> I can see that you can get the .Value property of XmlTextReader. But
C> I want
C> to be able to specify the node I want to look at.
C> e.g.
C>
C> <Group>
C> <Items>
C> <Item>content here</Item>
C> <Item>some more content here</Item>
C> <Items>
C> <Activity></Activity>
C> <Location></Location>
C> </Group>
C> thanks.
C>
C> CR
C>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Mar 13 '06 #3
et voila (note your xml was malformed); also, generally (for large xml) I
wouldn't use a string and MemoryStream here unless I had to - but it was the
only 1-line way I could think of to initialise the reader. Loading directly
from the uri (or a file) would be an option, though...

Marc

using System;
using System.Xml;
using System.Text;
using System.IO;
namespace ReadSomeXml {
class Program {
static void Main(string[] args)
{
string xml = @"<Group>
<Items>
<Item>content here</Item>
<Item>some more content here</Item>
</Items>
<Activity></Activity>
<Location></Location>
</Group>";
using(XmlReader reader = XmlReader.Create(new
MemoryStream(Encoding.UTF8.GetBytes(xml)))) {

while(true) {
if(reader.NodeType == XmlNodeType.Element && reader.Name
== "Item") {
string content =
reader.ReadElementContentAsString();
Console.WriteLine(content);
} else {
if(!reader.ReadToFollowing("Item")) break;
}
}
}
}
}
}
"Marc Gravell" <ma**@nonesuch.com> wrote in message
news:eN**************@TK2MSFTNGP09.phx.gbl...
you can use ReadToFollowing("Item") to jump to each Item - however, you
need to be careful, as the "intuitive" code might be (IIRC) wrong:

while(reader.ReadToFollowing("Item")) {
string content = reader.ReadElementContentAsString();
// do something with content
}

The reason this is wrong is that ReadElementContentAsString progresses
/past/ the [Item] element, so the next call to ReadToFollowing() might
skip the next row; you might be able-to do something with a sub-reader,
else you might need to check (after calling ReadElementContentAsString) if
you are on an Item element; if you are, it is the *next* one, so read it;
if you aren't, then ReadToFollowing

Marc

"CodeRazor" <Co*******@discussions.microsoft.com> wrote in message
news:C1**********************************@microsof t.com...
I am reading an xml file from a URL. I was originally doing this using
the
XmlDocument class. But this was very slow. The XmlTextReader is meant to
be
much quicker for forward only retrieval of data.

I need to somehow access a particular list of nodes. e.g. I want to loop
through all the nodes called <item></item>. How is this possible using
XmlTextReader?
I can see that you can get the .Value property of XmlTextReader. But I
want
to be able to specify the node I want to look at.

e.g.

<Group>
<Items>
<Item>content here</Item>
<Item>some more content here</Item>
<Items>
<Activity></Activity>
<Location></Location>
</Group>

thanks.

CR


Mar 13 '06 #4
Thanks for your help, but I am still stumped.

I have found an inelegant way of retrieving each item's title and
description using string methods. See my code below. I would like to know how
I can retrieve the <title> and <description> nodes more directly using the
methods of the XmlTextReader.

My xml file looks somthing like:
<items>
<item><title>Title 1</title><description>Description 1</description></item>
<item><title>Title 2</title><description>Description 2</description></item>
</items>

My Hack:
XmlTextReader xmlTextReader = new XmlTextReader(myXmlUrl);

while(xmlTextReader.Read())
{
if(xmlTextReader.NodeType == XmlNodeType.Element && xmlTextReader.Name ==
"item" )
{
string itemContent = xmlTextReader.ReadOuterXml();
int headlineStart = itemContent.IndexOf("<title>") + "<title>".Length;
headlineLength = itemContent.IndexOf("</title>") - headlineStart;
descriptStart = itemContent.IndexOf("<description>") +
"<description>".Length;
int descriptLength = itemContent.IndexOf("</description>") - descriptStart;
string headline = itemContent.Substring(headlineStart,
headlineLength);
string description = itemContent.Substring(descriptStart, descriptLength);

Response.Write(headline + "<br>" + description + "<br><br>");
}

Many thanks,

CR

Mar 14 '06 #5

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

Similar topics

4
by: Roshan | last post by:
I'm newbie to xml and C#. I have one XML file with the following content: <Store> <Book id="1" > <Title>Thermodynamics Unleashed</Title> <Price>56.00</Price> <Book>
3
by: Scott M. Lyon | last post by:
I'm trying to figure out the best way (considering there could be instances where I get a lot of data in this XML, and I want to minimize any slowdowns) to extract the value of one particular node...
0
by: Shafia | last post by:
Hi, How to find a node in a .Net framework TreeView like we do in Web TreeView as follows TreeView1.FindNode(myNode.ValuePath) There is a method TreeNode nn = treeView1.Nodes.Find(Key,...
9
by: anunaygupta | last post by:
Hello all, I have a data structures problem. Assume there are 2 linked lists L1 and L2. They merge into some node. L1 1->2->3->4->5->6 / L2 8->7->9->/ L1 and L2 merge in node with value...
0
by: saravanaVijayakumar | last post by:
I'm new to xml .. I had created a application such a way that I have to display the xml file in treeview control in C#.Net Application ... If I select the particular Node it should display the...
1
by: kunal30doshi | last post by:
hi, i want to find particular char. from entire char. field within given range. Ex Field x is 10 char field and it's value is YYYYNNNNYY. i want to know if n is exist in this field withing...
1
by: sai14 | last post by:
Hi all, i would like to know if it is possible find particular string in a text file using vc++? please do let me know how to write the syntax Thanks
14
by: vikaskumaragrawal | last post by:
hi friends, i have a question plz help me. my question is ... how can i find middle node of linked list in single traverse???????
3
by: Karthik01 | last post by:
Hi, i have written a VB script to find a node name in an XML.Now i need to search for that node name in another XML. How can i do it. My XML looks like this - <xml...
2
by: saran3b2 | last post by:
How can i find CLOSING NODE in XSL-FO, i want to generate some text in each </para> node. For Ex:- --------------XML File--------------- <list> <item><para>Some Text1</para></item>...
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
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
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
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.