tags:

views:

788

answers:

2

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.

A: 

You can just cast the returned XmlReader as an XmlTextReader.

ck
I have tried this and it doesn't work:XmlTextReader r2 = (XmlTextReader)r1.ReadSubtree();XmlTextReader is derived from XmlReader so if a XMLReader is returned it can't be cast to a derived class.
Gary Joynes
+2  A: 

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.

Rune Grimstad
I restructured things eventually but I believe this will work
Gary Joynes