473,320 Members | 2,202 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,320 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 2717


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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.