Search code examples
c#xmlreader

XMLTextReader.ReadSubtree to return XMLTextReader


I am using an XMLTextReader to process an XML file (as opposed to an XMLDocument). I use XMLTextReader so I can use the option to ignore all whitespace.

At certain points I want to read in a node using ReadSubtree. This returns an XMLReader. How can I convert this to a XMLTextReader so I can use the ignore whitespace option.


Solution

  • I think you should be able to use the XmlReader.ReadOuterXml() method to return the entire subtree as a string. Then you can use it to create a StringReader and use that again to create an XmlTestReader.

    Clumsy, but it should work.

    Oh. By the way. The ReadSubtree returns an XmlSubtreeReader which is derived from the XmlReader, so you can't cast it to an XmlTextReader directly.