JoeMjr2
JoeMjr2

Reputation: 3944

XDocument.CreateWriter using XmlWriterSettings

I'm trying to create an XMLWriter for an XDocument, and also apply settings to it, but I can't figure out how.

Here's what I have so far.

        var writerSettings = new XmlWriterSettings()
        {
            OmitXmlDeclaration = true,
            Encoding = Encoding.UTF8
        };
        var request = new XDocument();
        using (var writer = request.CreateWriter())
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("CUSTOMER");
            writer.WriteElementString("ADDRESS", "123 Fake St.");
            writer.WriteElementString("CITY", "San Jose");
            writer.WriteElementString("STATE", "CA");
            writer.WriteEndElement();
            writer.WriteEndDocument();
        }

I can't figure out how to apply writerSettings to the writer. The XDocument.CreateWriter() method doesn't take any parameters to specify the XMLWriterSettings. And, after it's created, the Settings property doesn't have a setter.

If there's no way to use CreateWriter() and apply settings, how else could I accomplish something equivalent, and end up with the same result?

Upvotes: 2

Views: 3072

Answers (2)

matrixanomaly
matrixanomaly

Reputation: 6947

Alternatively, use the static method of xmlwriter, which is create()

using (XmlWriter writer = XmlWriter.Create(filePath, settings))
{ 
//do your thing 
} 

I've ran into this problem before and I do agree it's not very obvious, especially when trying to do a new xmlwriter.

Edit: also possible duplicate of How do I set the Settings property in XmlTextWriter, so that I can write each XML attribute on its own line?

Which you can refer to for further reading if you'd like!

Upvotes: 1

softwariness
softwariness

Reputation: 4052

If you use XDocument.Save(XmlWriter) you can create your own XmlWriter supplying the XmlWriterSettings parameters, and write the contents of the XDocument to it.

Alternatively, you can just call one of the XDocument.ToString overloads which omit the XML declaration from the returned string (if that was all you wanted to configure).

Your example updated below to show both options:

using System;
using System.Text;
using System.Xml;
using System.Xml.Linq;

class Program
{
    static void Main(string[] args)
    {
        var request = new XDocument();

        using (var writer = request.CreateWriter())
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("CUSTOMER");
            writer.WriteElementString("ADDRESS", "123 Fake St.");
            writer.WriteElementString("CITY", "San Jose");
            writer.WriteElementString("STATE", "CA");
            writer.WriteEndElement();
            writer.WriteEndDocument();
        }

        // XDocument.ToString() will print:
        //<CUSTOMER>
        //  <ADDRESS>123 Fake St.</ADDRESS>
        //  <CITY>San Jose</CITY>
        //  <STATE>CA</STATE>
        //</CUSTOMER>
        Console.WriteLine(request.ToString());

        // And here's how to use the XmlWriterSettings with XDocument.Save:
        var writerSettings = new XmlWriterSettings()
        {
            OmitXmlDeclaration = true,
            Encoding = Encoding.UTF8
        };

        using (var writer = XmlWriter.Create("test.xml", writerSettings))
        {
            request.Save(writer);
        }

        // The above will write (you could adjust the XmlWriterSettings to add whitespace):
        //<CUSTOMER><ADDRESS>123 Fake St.</ADDRESS><CITY>San Jose</CITY><STATE>CA</STATE></CUSTOMER>
    }
}

Or if you're not using any other functionality of XDocument, you could just create your own XmlWriter instance in the first place, and add the nodes to that, and leave out the XDocument interaction altogether.

Upvotes: 2

Related Questions