Many .NET functions use XmlWriter to output / generate xml. Output to a file / line / memory is a very important operation:
XmlWriter xw = XmlWriter.Create(PutYourStreamFileWriterEtcHere); xw.WriteStartElement("root"); ...
Sometimes you need to manipulate the resulting Xml and therefore want to load it into an XmlDocument or you may need an XmlDocument for some other reason, but you have to generate the XML using XmlWriter. For example, if you call a function in a third-party library, which is displayed only in XmlWriter.
One of the things you can do is write xml to a string and then load it into your XmlDocument:
StringWriter S = new StringWriter(); XmlWriter xw = XmlWriter.Create(S); XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(S.ToString());
However, this is inefficient - first you serialize all the xml information into a string, then parse the string again to create the DOM.
How can you tell XmlWriter to directly build an XmlDocument?
Boaz Aug 28 '09 at 13:32 2009-08-28 13:32
source share