Setting StandAlone = Yes in .Net when serializing an object

10,764

Solution 1

If you want to do this then you'll need to use WriteProcessingInstruction method and manually write it out.

    Using stream As New IO.MemoryStream, xtWriter As Xml.XmlWriter = Xml.XmlWriter.Create(stream, settings)
        xtWriter.WriteProcessingInstruction("xml", "version=""1.0"" encoding=""UTF-8"" standalone=""yes""")
        serializer.Serialize(xtWriter, obj)
        Return encoding.GetString(stream.ToArray())
    End Using

Solution 2

I've found a much more elegant way of doing this: simply call WriteStartDocument(true) on your XmlWriter instance - this code serializes data and outputs the resulting XML to console.

First, if you're using a StringWriter you need to tweak it a bit to force UTF-8, but keep this in mind:

When serialising an XML document to a .NET string, the encoding must be set to UTF-16. Strings are stored as UTF-16 internally, so this is the only encoding that makes sense. If you want to store data in a different encoding, you use a byte array instead.

public sealed class Utf8StringWriter : StringWriter
{
    public override Encoding Encoding { get { return Encoding.UTF8; } }
}
using (var sw = new Utf8StringWriter())
using (var xw= XmlWriter.Create(sw, new XmlWriterSettings{Indent = true}))
{
    xw.WriteStartDocument(true); // that bool parameter is called "standalone"

    var namespaces = new XmlSerializerNamespaces();
    namespaces.Add(string.Empty, string.Empty);

    var xmlSerializer = new XmlSerializer(typeof(data));
    xmlSerializer.Serialize(xw, data);

    Console.WriteLine(sw.ToString());
}

WriteStartDocument(true) really feels like the idiomatic way of specifying standalone=true. The output heading looks like this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
Share:
10,764
Mr Shoubs
Author by

Mr Shoubs

Hello, A Software Developer for BTC Solutions, spend my day keeping a shipload of pirates in order. http://uk.linkedin.com/pub/daniel-shoubridge/13/338/470/

Updated on June 19, 2022

Comments

  • Mr Shoubs
    Mr Shoubs almost 2 years

    In the following code I want to set "standalone = yes" to the xml, how can I do this?

    Dim settings As New Xml.XmlWriterSettings
    settings.Encoding = encoding
    
    Using stream As New IO.MemoryStream, xtWriter As Xml.XmlWriter = _
        Xml.XmlWriter.Create(stream, settings)
        serializer.Serialize(xtWriter, obj)
        Return encoding.GetString(stream.ToArray())
    End Using
    

    For example, I have this:

    <?xml version="1.0" encoding="utf-8"?>
    

    But I want this:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>