473,386 Members | 1,819 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,386 software developers and data experts.

XmlTextReader Question

Bob
Hi,

I am parsing a xml string for a specific element using XmlTextReader (code
below). I now need to change it to get the element's value plus get the
parent node's name. Can someone help me?

Thanks

public object GetResponseValue(string xml, string element)
{
using (XmlTextReader reader = new XmlTextReader(new
System.IO.StringReader(xml)))
{
object elementValue = null;
object elementTag = reader.NameTable.Add(element);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name.Equals(elementTag))
{
elementValue = reader.ReadElementContentAsObject();
break;
}
}
}
return elementValue;
}
}

Oct 6 '06 #1
4 2721


Bob wrote:

I am parsing a xml string for a specific element using XmlTextReader (code
below). I now need to change it to get the element's value plus get the
parent node's name.
Xml(Text)Reader provides a fast _forwards only_ pull parsing approach
meaning once you are at a certain node or element node you can't go back
to determine the parent or parent name. Thus your code needs to store
the parent name while it pulls in the parent. If you don't know anything
about the possible parents before you find the child then you will need
to store the potential parent name any time you pull in an element.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 8 '06 #2
Bob
Thanks. How do I pull the parent name?
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:Op**************@TK2MSFTNGP03.phx.gbl...
>

Bob wrote:

>I am parsing a xml string for a specific element using XmlTextReader
(code
below). I now need to change it to get the element's value plus get the
parent node's name.

Xml(Text)Reader provides a fast _forwards only_ pull parsing approach
meaning once you are at a certain node or element node you can't go back
to determine the parent or parent name. Thus your code needs to store the
parent name while it pulls in the parent. If you don't know anything about
the possible parents before you find the child then you will need to store
the potential parent name any time you pull in an element.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Oct 9 '06 #3
Bob wrote:
Thanks. How do I pull the parent name?
Just store current element name each time you read next one.

--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
Oct 9 '06 #4


Bob wrote:
How do I pull the parent name?
When the reader reads in an element you have to store it as a potential
parent name you might need later. I think a Stack<stringis a good data
structure to store the element names, then store the Depth of the reader
too and check the reader's Depth property and if it increases push the
current element name, if it stays constant pop the last name from the
stack and push the current element name, if the depth decreases then pop
the last name from the stack.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 9 '06 #5

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

Similar topics

1
by: Gustaf Liljegren | last post by:
I shall make a make a function that will read an XML file, looking for elements like this <context id="A1"> <period> <instant>2001-01-01</instant> </period> </context> or this
4
by: Andy Neilson | last post by:
I've run across a strange behaviour with XmlSerializer that I'm unable to explain. I came across this while trying to use XmlSerializer to deserialize from a the details of a SoapException. This...
7
by: SQLScott | last post by:
I have a Web Service in which I am trying to pass an XMLDocument as a parameter to one of the methods. I would like to use the XMLTextReader to read the XML but I am getting the following error: ...
3
by: Newton | last post by:
Hi, I receive XML document from input and I would like to read it several times... My declaration is XmlTextReader XMLInputReader = new XmlTextReader(fileImport.PostedFile.InputStream); I...
1
by: SHC | last post by:
Hi all, I did the "Build" on the attached code in my VC++ .NET 2003 - Windows XP Pro PC. On the c:\ screen, I got the following: Microsoft Development Environment An unhandled exception of type...
1
by: Raed Sawalha | last post by:
I have the following XML i do the following to extract the name information XmlTextReader Reader = new XmlTextReader("C:\\names.xml"); while(tempReader.Read() ) { if(tempReader.Name.ToLower()...
1
by: Nick | last post by:
Hi All, if i have the following code: StringReader stringReader = new StringReader(c_XML); XmlTextReader xmlReader = new XmlTextReader(stringReader); If I call 'xmlReader.Close();' I take it...
2
by: XML reading with XMLTextReader | last post by:
im trying to read an xml file which is in the wwwroot folder.im using IIS on XP Prof. my code is...
4
by: CodeRazor | last post by:
I am trying to use an XmlTextReader to retrieve data. I need to use an XmlTextReader because it is faster than using an XmlDocument. I have found an inelegant way of retrieving each item's title...
2
by: =?Utf-8?B?TWlzY2hh?= | last post by:
It is my understanding that I can create an XmlTextReader from a variety of sources, including a stream object. I am developing a web application that reads some data from an Xml file. Everything...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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...

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.