Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting StandAlone = Yes in .Net when serializing an object

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"?>
like image 835
Mr Shoubs Avatar asked Aug 09 '11 13:08

Mr Shoubs


People also ask

What does XML standalone mean?

The XML standalone element defines the existence of an externally-defined DTD. In the message tree it is represented by a syntax element with field type XML. standalone. The value of the XML standalone element is the value of the standalone attribute in the XML declaration.

Which class should be used to serialize an object in XML format?

XmlIgnoreAttribute Class (System. Xml. Serialization) Instructs the Serialize(TextWriter, Object) method of the XmlSerializer not to serialize the public field or public read/write property value.

Is XmlSerializer thread safe?

Since XmlSerializer is one of the few thread safe classes in the framework you really only need a single instance of each serializer even in a multithreaded application.

What is XmlSerializer?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.


1 Answers

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"?>
like image 175
Mathieu Guindon Avatar answered Sep 21 '22 08:09

Mathieu Guindon