GurdeepS
GurdeepS

Reputation: 67283

Write out xmlwriter to file

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

Upvotes: 14

Views: 36938

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

One possibility is to set the XmlWriter to output to a text file:

using (var writer = XmlWriter.Create("dump.xml"))
{
    ...
}

Upvotes: 4

user2140261
user2140261

Reputation: 8003

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

Upvotes: 15

Related Questions