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

czuroski
  • 4,316
  • 9
  • 50
  • 86

4 Answers4

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);
}
Achim
  • 15,415
  • 15
  • 80
  • 144
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=");
  ...
Petr Havlicek
  • 2,051
  • 1
  • 19
  • 25
  • 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! – CodeRedick Jan 09 '12 at 05:03
  • 1
    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](http://www.w3.org/TR/xml11/#sec-prolog-dtd) it is perfectly legal to not include an encoding declaration inside the XML declaration. In that case the XML declaration looks like: `` (or `` for XML 1.0). – Jeppe Stig Nielsen Sep 07 '15 at 13:17
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)
taylor michels
  • 460
  • 5
  • 12
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\"?>", "");
kroehre
  • 1,104
  • 5
  • 15
  • 1
    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. – czuroski Jun 09 '11 at 18:43
  • I am interested in the same thing. Anyone has an answer? I used: StringBuilder.ToString().Replace("<", "<").Replace(">", ">"); because I had some html code which I wanted not encoded. – sebastian.roibu May 22 '17 at 05:30