XmlTextReader error

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

XmlTextReader XmlRdr = new System.Xml.XmlTextReader(XmlFile);

//while moving through the xml document.
while(XmlRdr.Read())
{
//check the node type and look for the element type
if (XmlRdr.NodeType==XmlNodeType.Element)
{XMLOutput.AppendText(XmlRdr.ReadSubtree() + "\r\n");}
}

Granted I don't really know what I'm doing quite yet :) , but getting
the error message below doesn't make much sense to me because I am
making sure the reader is on an element node.

System.InvalidOperationException was unhandled
Message="ReadSubtree() can be called only if the reader is on an
element node."


Tony!
 
Since that won't compile (string conversion) and doesn't have sample
xml, it is hard to give an absolute answer - however this might help...
perhaps... although note that using a subtree just to read the outer
xml is overkill ;-p

If you post with more info, you may get a better answer... hope this
helps, though:

Marc

using System;
using System.Xml;
using System.IO;
using System.Diagnostics;

class Program {

static void Main() {
const string xml = @"
<xml att=""test""><data a=""b""/><a/><b/><c><d><e/></d><f/></c></xml>";
using(StringReader sr = new StringReader(xml))
using(XmlReader xr = XmlReader.Create(sr)) {
xr.MoveToContent();
while (xr.Read()) {
Debug.WriteLine(xr.NodeType, "in:" + xr.Name);
if (xr.NodeType == XmlNodeType.Element) {
using (XmlReader subtree = xr.ReadSubtree()) {
subtree.MoveToContent();
Debug.WriteLine(subtree.ReadOuterXml());
}
}
Debug.WriteLine(xr.NodeType, "out:" + xr.Name);
}
}

}
}
 
Back
Top