473,804 Members | 3,031 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 44966
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.Re adToFollowing(" Item")) {
string content = reader.ReadElem entContentAsStr ing();
// do something with content
}

The reason this is wrong is that ReadElementCont entAsString 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 ReadElementCont entAsString) 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*******@disc ussions.microso ft.com> wrote in message
news:C1******** *************** ***********@mic rosoft.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.NodeTyp e)
check the case XmlNodeType.Ele ment 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.Creat e(new
MemoryStream(En coding.UTF8.Get Bytes(xml)))) {

while(true) {
if(reader.NodeT ype == XmlNodeType.Ele ment && reader.Name
== "Item") {
string content =
reader.ReadElem entContentAsStr ing();
Console.WriteLi ne(content);
} else {
if(!reader.Read ToFollowing("It em")) break;
}
}
}
}
}
}
"Marc Gravell" <ma**@nonesuch. com> wrote in message
news:eN******** ******@TK2MSFTN GP09.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.Re adToFollowing(" Item")) {
string content = reader.ReadElem entContentAsStr ing();
// do something with content
}

The reason this is wrong is that ReadElementCont entAsString 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 ReadElementCont entAsString) 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*******@disc ussions.microso ft.com> wrote in message
news:C1******** *************** ***********@mic rosoft.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>Ti tle 1</title><descript ion>Description 1</description></item>
<item><title>Ti tle 2</title><descript ion>Description 2</description></item>
</items>

My Hack:
XmlTextReader xmlTextReader = new XmlTextReader(m yXmlUrl);

while(xmlTextRe ader.Read())
{
if(xmlTextReade r.NodeType == XmlNodeType.Ele ment && xmlTextReader.N ame ==
"item" )
{
string itemContent = xmlTextReader.R eadOuterXml();
int headlineStart = itemContent.Ind exOf("<title>") + "<title>".Lengt h;
headlineLength = itemContent.Ind exOf("</title>") - headlineStart;
descriptStart = itemContent.Ind exOf("<descript ion>") +
"<description>" .Length;
int descriptLength = itemContent.Ind exOf("</description>") - descriptStart;
string headline = itemContent.Sub string(headline Start,
headlineLength) ;
string description = itemContent.Sub string(descript Start, 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
6053
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
1886
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 from an XML string (not saved as a file, but passed as a string from another module). For example, let's assume I get back XML in a string that looks like this: <Commands> <cmd name="1">Value 1</cmd>
0
1095
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, true);
9
3039
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 5. We need to return the pointer to
0
2190
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 attributes of that node in DataGrid But I am displaying the attributes in dataGrid if i use the following Code XmlNode x = root.SelectSingleNode("//JDF")
1
1832
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 given range.
1
2471
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
37515
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
1975
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 xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema"> - <s:Schema id="RowsetSchema"> - <s:ElementType name="row" content="eltOnly"> - <s:AttributeType...
2
2341
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> <item><para>Some Text2</para></item> <item><para>Some Text3</para></item> </list> -------------------------------------
0
10564
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10320
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10308
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7609
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.