20

I have this bit of code, which serializes an object to a file. I'm trying to get each XML attribute to output on a separate line. The code looks like this:

public static void ToXMLFile(Object obj, string filePath)
{
    XmlSerializer serializer = new XmlSerializer(obj.GetType());

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.NewLineOnAttributes = true;

    XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8);
    writer.Settings = settings; // Fails here.  Property is read only.

    using (Stream baseStream = writer.BaseStream)
    {
        serializer.Serialize(writer, obj);
    }
}

The only problem is, the Settings property of the XmlTextWriter object is read-only.

How do I set the Settings property on the XmlTextWriter object, so that the NewLineOnAttributes setting will work?


Well, I thought I needed an XmlTextWriter, since XmlWriter is an abstract class. Kinda confusing if you ask me. Final working code is here:

/// <summary>
/// Serializes an object to an XML file; writes each XML attribute to a new line.
/// </summary>
public static void ToXMLFile(Object obj, string filePath)
{
    XmlSerializer serializer = new XmlSerializer(obj.GetType());

    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.NewLineOnAttributes = true;

    using (XmlWriter writer = XmlWriter.Create(filePath, settings))
    {
        serializer.Serialize(writer, obj);
    }
}
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501

2 Answers2

23

Use the static Create() method of XmlWriter.

XmlWriter.Create(filePath, settings);

Note that you can set the NewLineOnAttributes property in the settings.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Polity
  • 14,734
  • 2
  • 40
  • 40
  • It didn't instantiate. It says it executed, but the object so created is null. Note: I used `XmlTextWriter writer = XmlWriter.Create(filePath, settings) as XmlTextWriter;` – Robert Harvey Nov 23 '11 at 04:18
  • @RobertHarvey - That means that `XmlWriter.Create(...)` doesn't create a `XmlTextWriter`. When you look at the output, you'll find that it returns a `XmlWellFormedWriter`. It would be wise though to treat it like an `XmlWriter`. – Polity Nov 23 '11 at 04:22
  • So how do I get it to an `XmlTextWriter`? – Robert Harvey Nov 23 '11 at 04:23
  • 1
    Never mind, got it working, thanks. You were right, I just needed an `XmlWriter`. The `XmlWriter` class is an `abstract` class, but you can create instances of it anyway. And the instances so created are called something else. That's just bizarre. – Robert Harvey Nov 23 '11 at 04:35
5

I know the question is old, anyway it's actually possible to set indentation for the XMLTextWriter. Unlike with the XMLwriter, you don't have to pass through the settings; you should use the Formatting property:

XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8);
w.Formatting = Formatting.Indented; 

See https://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.formatting(v=vs.110).aspx

alelom
  • 2,130
  • 3
  • 26
  • 38