How to create an XmlDocument using XmlWriter in .NET?

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); /* write away */ 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?

+45
xmldocument xmlwriter
Aug 28 '09 at 13:32
source share
5 answers

Here is at least one solution:

 XmlDocument doc = new XmlDocument(); using (XmlWriter writer = doc.CreateNavigator().AppendChild()) { // Do this directly  writer.WriteStartDocument();  writer.WriteStartElement("root");  writer.WriteElementString("foo", "bar");  writer.WriteEndElement();  writer.WriteEndDocument(); // or anything else you want to with writer, like calling functions etc. } 

Apparently XpathNavigator gives you an XmlWriter when calling AppendChild ()

Credits are sent to Martin Honnen: http://groups.google.com/group/microsoft.public.dotnet.xml/browse_thread/thread/24e4c8d249ad8299?pli=1

+76
Aug 28 '09 at 14:28
source share
β€” -

You can do the opposite: first create an XmlDocument using the DOM, then write it to XmlWriter :

 XmlDocument xdoc = new XmlDocument(); ... // build the document StringWriter S = new StringWriter(); XmlWriter xw = XmlWriter.Create(S); xdoc.WriteTo(xw); 
+8
Aug 28 '09 at 13:41
source share

You can write an xml file using the XMLWriter class. Here is an example for this.

  XmlWriterSettings objSetting = new XmlWriterSettings(); objSetting.Indent = true; objSetting.NewLineOnAttributes = true; System.Text.StringBuilder sb = new System.Text.StringBuilder(); using (XmlWriter objWriter = XmlWriter.Create(sb, objSetting)) { //Note the artificial, but useful, indenting objWriter.WriteStartDocument(); objWriter.WriteStartElement("books"); ////////Start Book Element/////// objWriter.WriteStartElement("book"); objWriter.WriteStartAttribute("ISBN"); objWriter.WriteValue("asp1"); objWriter.WriteEndAttribute(); objWriter.WriteStartElement("Title"); objWriter.WriteValue("ASP.NET"); objWriter.WriteEndElement(); objWriter.WriteElementString("ReleaseDate", "11/11/2010"); objWriter.WriteStartElement("Pages"); objWriter.WriteValue(200); objWriter.WriteEndElement(); //price objWriter.WriteEndElement(); //book ////////End Book Element/////// ////////Another Element ////////Start Book Element/////// objWriter.WriteStartElement("book"); objWriter.WriteStartAttribute("ISBN"); objWriter.WriteValue("c#2"); objWriter.WriteEndAttribute(); objWriter.WriteStartElement("Title"); objWriter.WriteValue("C#.NET"); objWriter.WriteEndElement(); objWriter.WriteElementString("ReleaseDate", "10/11/2010"); objWriter.WriteStartElement("Pages"); objWriter.WriteValue(500); objWriter.WriteEndElement(); objWriter.WriteEndElement(); //book ////////End Book Element/////// objWriter.WriteEndElement(); //books objWriter.WriteEndDocument(); } File.WriteAllText(Server.MapPath("BooksList.xml"), sb.ToString()); 
+6
Jul 23 '12 at 9:10
source share

The idea of ​​XmlWriter is to wait until you finish modifying your data before writing.

XmlWriter was not created to suit your situation.

Or

  • Wait until you know what your data is before writing

or

  • Do what you do now
+4
Aug 28 '09 at 13:45
source share

There is a basic Stream object that XmlWriter wrote, if it was bidirectional (MemoryStream), you can simply rearrange it back to -0- and then use the Stream object in XmlDocument.Load (stream).

NTN

Z

+1
Aug 28 '09 at 13:46
source share



All Articles