19

I am using the following code to create an xml document -

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
new XmlSerializer(typeof(docket)).Serialize(Console.Out, i, ns); 

this works great in creating the xml file with no namespace attributes. i would like to also have no encoding attribute in the root element, but I cannot find a way to do it. Does anyone have any idea if this can be done?

Thanks

4 Answers 4

34

Old answer removed and update with new solution:

Assuming that it's ok to remove the xml declaration completly, because it makes not much sense without the encoding attribute:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", "");
using (XmlWriter writer = XmlWriter.Create(Console.Out, new XmlWriterSettings { OmitXmlDeclaration = true}))
{
  new XmlSerializer(typeof (SomeType)).Serialize(writer, new SomeType(), ns);
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is more of a comment than a proper answer... Try to work on your answer or you might be downvoted for that
I agree and have modified it into a better one. ;-)
this worked, but I wonder why its not beautifying the output, vs when you serialize a FileStream instead of XmlWriter
11

To remove encoding from XML header pass TextWriter with null encoding to XmlSerializer:

MemoryStream ms = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(ms, null);
s.Serialize(w, vs);

Explanation

XmlTextWriter uses encoding from TextWriter passed in constructor:

// XmlTextWriter constructor 
public XmlTextWriter(TextWriter w) : this()
{
  this.textWriter = w;
  this.encoding = w.Encoding;
  ..

It uses this encoding when generating XML:

// Snippet from XmlTextWriter.StartDocument
if (this.encoding != null)
{
  builder.Append(" encoding=");
  ...

2 Comments

Oddly, Achim's answer caused other errors with my serialization... XmlTextWriter.Create() doesn't behave the same was as "new XmlTextWriter()" and was giving me null reference errors. This technique worked nicely though so thanks!
Using an encoding which is null is a good way to achieve this. Note that according to section 2.8 of the XML 1.1 Recommendation it is perfectly legal to not include an encoding declaration inside the XML declaration. In that case the XML declaration looks like: <?xml version="1.1"?> (or <?xml version="1.0"?> for XML 1.0).
1
string withEncoding;       
using (System.IO.MemoryStream memory = new System.IO.MemoryStream()) {
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(memory)) {
        serializer.Serialize(writer, obj, null);
        using (System.IO.StreamReader reader = new System.IO.StreamReader(memory)) {
            memory.Position = 0;
            withEncoding= reader.ReadToEnd();
        }
    }
}

string withOutEncoding= withEncoding.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");

2 Comments

using a string.Replace could work, but I was wondering if there was a way to do it with the xmlserializer, as I did above to remove the namespace.
I am interested in the same thing. Anyone has an answer? I used: StringBuilder.ToString().Replace("&lt;", "<").Replace("&gt;", ">"); because I had some html code which I wanted not encoded.
1

Credit to this blog for helping me with my code http://blog.dotnetclr.com/archive/2008/01/29/removing-declaration-and-namespaces-from-xml-serialization.aspx

here's my solution, same idea, but in VB.NET and a little clearer in my opinion.

Dim sw As StreamWriter = New, StreamWriter(req.GetRequestStream,System.Text.Encoding.ASCII)
Dim xSerializer As XmlSerializer = New XmlSerializer(GetType(T))
Dim nmsp As XmlSerializerNamespaces = New XmlSerializerNamespaces()
nmsp.Add("", "")

Dim xWriterSettings As XmlWriterSettings = New XmlWriterSettings()
xWriterSettings.OmitXmlDeclaration = True

Dim xmlWriter As XmlWriter = xmlWriter.Create(sw, xWriterSettings)
xSerializer.Serialize(xmlWriter, someObjectT, nmsp)

Comments

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.