andy
andy

Reputation: 8875

How to read from an XmlReader without moving it forwards?

I 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 not 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?

Upvotes: 14

Views: 15072

Answers (6)

stef
stef

Reputation: 71

Something like that with ReadSubtree

using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
                                {
                                    reader.MoveToContent();
                                    while (reader.Read())
                                    {
                                        switch (reader.NodeType)
                                        {
                                            case XmlNodeType.Element:
                                                if (reader.Name == "Field") // for example i need to read node field
                                                {

                                                    XmlReader inner = reader.ReadSubtree();  // the reader stays in the same  position
                                                    XElement El = XElement.Load(inner) as XElement;
                                                    inner.Close();
                                                }
                                        }
                                    }
                                }

Upvotes: -1

Priyadarshini Gorur
Priyadarshini Gorur

Reputation: 21

What I did was read just the element into an XmlDocument, and read that instead. In my case, I had to convert a flow document to HTML. I had to read a inner element to assign a "style" to the parent HTML element.

Upvotes: 1

CRD
CRD

Reputation: 51

Another idea: read the outer XML (which advances the reader), then create a new reader from this XML which allows you to "go back" and process the elements of the current node.

while (r.ReadToFollowing("ParentNode"))
{
    parentXml = r.ReadOuterXml();

    //since ReadOuterXml() advances the reader to the next parent node, create a new reader to read the remaining elements of the current parent
    XmlReader r2 = XmlReader.Create(new StringReader(parentXml));
    r2.ReadToFollowing("ChildNode");
    childValue = r2.ReadElementContentAsString();
    r2.Close();
}                  

Upvotes: 5

a_f
a_f

Reputation: 36

In fact, although Vitek Karas MSFT is right, Helena Kupkova published a deft little XML Bookmark Reader on https://msdn.microsoft.com/en-us/library/aa302292.aspx. This enables going backwards using caching.

Upvotes: 0

Vitek Karas MSFT
Vitek Karas MSFT

Reputation: 13320

Actually ReadSubtree will return a reader which "wraps" the original reader. So reading through the new one will end up advancing the original one as well. You must consider XmlReader as a forward only reader, it simply can't go back. As for your scenario, instead of trying to remember part of the XML you can ask the reader for the position in the input file. Just cast it to IXmlLineInfo interface, it has methods to return line and position. Using this you could remember some starting position (before the element in question) and then the end position of the error. And then read that part from the intput file as a plain text.

Upvotes: 11

Flynn1179
Flynn1179

Reputation: 12075

Don't use any 'Read' operation on the reader- as you've discovered, this advances it. Use calls to properties such as reader.HasValue and reader.Value to inspect the contents. Look up 'XmlReader' in the object browser, there's quite a few properties you can read.

Edit: I don't think there's an easy way of simply getting the XML, possibly because the current node may not be valid XML on it's own, such as an XmlWhiteSpace, XmlText node or even an XmlAttribute.

Upvotes: 4

Related Questions