Many .NET functions use XmlWriter to output/generate xml. Outputting to a file/string/memory is a very operation:
XmlWriter xw = XmlWriter.Create(PutYourStreamFileWriterEtcHere);
xw.WriteStartElement("root");
...
Sometimes , you need to manipulate the resulting Xml and would therefore like to load it into a XmlDocument or might need an XmlDocument for some other reason but you must generate the XML using an XmlWriter. For example, if you call a function in a 3rd party library that outputs to a XmlWriter only.
One of the things you can do is write the xml to a string and then load it into your XmlDocument:
StringWriter S = new StringWriter();
XmlWriter xw = XmlWriter.Create(S);
/* write away */
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(S.ToString());
However this is inefficient - first you serialize all the xml info into a string, then you parse the string again to create the DOM.
How can you point an XmlWriter to build a XmlDocument directly?
You could do the opposite : build the
XmlDocument
first using DOM, then write it to aXmlWriter
:There is an underlying Stream object that the XmlWriter was writing to, if it was bidirectional (MemoryStream), you could simply re-position it back to -0- and then use the Stream object in the XmlDocument.Load(stream).
HTH,
Z
Here's at least one solution:
Apparently XpathNavigator gives you a XmlWriter when you call AppendChild()
Credits go to Martin Honnen on : http://groups.google.com/group/microsoft.public.dotnet.xml/browse_thread/thread/24e4c8d249ad8299?pli=1
You can write xml file using XMLWriter class. Here is example for this.
The idea behind XmlWriter is to wait until you have finished modifying your data before you start writing.
XmlWriter wasn't built with your situation in mind.
Either
or