Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write out xmlwriter to file

Tags:

c#

I have an xmlwriter object used in a method. I'd like to dump this out to a file to read it. Is there a straightforward way to do this?

Thanks

like image 543
GurdeepS Avatar asked Mar 07 '13 16:03

GurdeepS


People also ask

How to write XML using XmlWriter in c#?

using System. Xml; var sts = new XmlWriterSettings() { Indent = true, }; using XmlWriter writer = XmlWriter. Create("data. xml", sts); writer.

How do you create an XmlWriter?

Creates a new XmlWriter instance using the stream and XmlWriterSettings object. Creates a new XmlWriter instance using the specified XmlWriter and XmlWriterSettings objects. Creates a new XmlWriter instance using the specified StringBuilder. Creates a new XmlWriter instance using the specified filename.

What is XmlWriter?

The XmlWriter class writes XML data to a stream, file, text reader, or string. It supports the W3C Extensible Markup Language (XML) 1.0 (fourth edition) and Namespaces in XML 1.0 (third edition) recommendations.


1 Answers

Use this code

        // Create the XmlDocument.
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<item><name>wrench</name></item>");

        // Add a price element.
        XmlElement newElem = doc.CreateElement("price");
        newElem.InnerText = "10.95";
        doc.DocumentElement.AppendChild(newElem);

        // Save the document to a file and auto-indent the output.
        XmlTextWriter writer = new XmlTextWriter(@"C:\data.xml", null);
        writer.Formatting = Formatting.Indented;
        doc.Save(writer);

As found on MSDN: http://msdn.microsoft.com/en-us/library/z2w98a50.aspx

like image 200
user2140261 Avatar answered Oct 16 '22 07:10

user2140261