XmlTextReader, XmlReader

  • Thread starter Thread starter Random
  • Start date Start date
R

Random

Besides using the ExecuteXmlReader method from an ADO.NET Command object,
how would one load XML data into an XmlTextReader or XmlReader object? I
have an XmlReader, but I need to pull out one of the nodes into another
XmlReader, or XmlTextReader object. Please help!
 
Hi,

You will have to iterate through your existing XMLReader. Once u reach the node which u are looking for, use the ReadOuterXml() method to read the node and pass that as an argument to another XmlTextReader() object eg.

XmlTextReader reader2=new XmlTextReader(reader1.readOuterXml())
 
Tried that, it didn't work. My guess is that since ReadOuterXml returns a
string, it is invalid as an assignment to a new XmlTextReader.


Bharat Biyani said:
Hi,

You will have to iterate through your existing XMLReader. Once u reach the
node which u are looking for, use the ReadOuterXml() method to read the node
and pass that as an argument to another XmlTextReader() object eg.
 
Yep, it does accept a string but then you have to provide few other parameters also in that function.
The foll. will solve your problem: What it is doing is that it is converting a string to a stream which is accepted by the reader.

string xml_fragment=reader1.ReadOuterXml();
bytes = System.Text.Encoding.ASCII.GetBytes(xml_fragment);
stream=new MemoryStream(bytes);
reader2=new XmlTextReader(stream);
 
Back
Top