How to read from an XmlReader without moving it forwards

Posted by andy on Stack Overflow See other posts from Stack Overflow or by andy
Published on 2010-06-01T06:48:10Z Indexed on 2010/06/01 6:53 UTC
Read the original article Hit count: 240

Filed under:
|
|
|
|

hey guys, I've got this scenario:

while (reader.Read())
{
    if (reader.NodeType == XmlNodeType.Element && reader.Name == itemElementName)
    {
        XElement item = null;
        try
        {
            item = XElement.ReadFrom(reader) as XElement;
        }
        catch (XmlException ex)
        {
           //log line number and stuff from XmlException class  
        }
    }
}

In the above loop I'm transforming a certain node (itemElementName) into an XElement.

Some nodes will be good XML and will go into an XElement, however, some will not.

In the CATCH, I'd like to now only catch the standard XmlException stuff... I'd also like to catch an extract of the current Xml and a string.

However, if I do any kind of READ operation on the node before I pass it to the XElement, it moves the reader forward.

How can get a "snapshot" of the contents of the OuterXml of the reader without interfering with it's position?

© Stack Overflow or respective owner

Related posts about Xml

Related posts about .net-3.5