12

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"?>

2 Answers 2

24

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"?>
Sign up to request clarification or add additional context in comments.

3 Comments

Perhaps xw.WriteStartDocument(standalone:=true) would be a good use of named args?
In this code example, looks like 'namespaces' variable as a 3rd parameter to Serialize function was missed? I think xmlSerializer.Serialize(xw, data) should be changed to xmlSerializer.Serialize(xw, data, namespaces) if you want name spaces to be removed. Thanks.
@DiwakarPadmaraja possibly - the variable seems useless.... but the serialization works as intended as-is anyway.
7

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

3 Comments

Thank you, that's still not as elegant as I would have hoped, but it is still better than .Replace("<?xml version=""1.0"" encoding=""utf-8""?>", "<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>"). lol
You can use xmlriter.WriteStartDocument(true);
@HasaniBlackwell ugh, just now I notice that comment....... that would have been the answer I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.