Let's say I have a XmlWriter object, is it possible to convert it to a XmlDocument or String without writing to the HD and then streaming it back? Is there a better way?
XmlTextWriter w = new XmlTextWriter("test.xml",null);
w.WriteStartDocument();
...
w.WriteEndDocument();
w.Close();
StreamReader sr = File.OpenText("test.xml");
string input;
string s;
while ((input=sr.ReadLine()) != null)
{
s += input;
}
sr.Close();
XmlWriter is only an abstract base class, and there can be multiple implementations of XmlWriter. One of these is XmlTextWriter, which can write XML as text to a file, stream, or an inner (nested) text writer. One of the built-in TextWriter classes is called
StringWriter, which writes to a string that you can then obtain. To use it, try the following example code:
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
// ... write XML, and then flush the writer
xmlWriter.Flush();
// Return text from string writer.
return stringWriter.ToString();
Shanku Niyogi
This posting is provided "AS IS" with no warranties, and confers no rights.
mxmissile
Participant
1617 Points
372 Posts
XmlWriter Object
Sep 26, 2003 07:53 PM|LINK
XmlTextWriter w = new XmlTextWriter("test.xml",null); w.WriteStartDocument(); ... w.WriteEndDocument(); w.Close(); StreamReader sr = File.OpenText("test.xml"); string input; string s; while ((input=sr.ReadLine()) != null) { s += input; } sr.Close();ShankuN
Participant
1455 Points
287 Posts
Microsoft
Re: XmlWriter Object
Sep 27, 2003 09:53 PM|LINK
This posting is provided "AS IS" with no warranties, and confers no rights.