468,249 Members | 1,467 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

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 2427


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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Gustaf Liljegren | last post: by
4 posts views Thread by Andy Neilson | last post: by
3 posts views Thread by Newton | last post: by
1 post views Thread by Raed Sawalha | last post: by
1 post views Thread by Nick | last post: by
2 posts views Thread by XML reading with XMLTextReader | last post: by
4 posts views Thread by CodeRazor | last post: by
2 posts views Thread by =?Utf-8?B?TWlzY2hh?= | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.