Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove encoding from xmlserializer

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

like image 956
czuroski Avatar asked Jun 09 '11 18:06

czuroski


People also ask

How to remove encoding from Xml in c#?

Add("", ""); new XmlSerializer(typeof(docket)). Serialize(Console. Out, i, ns); this works great in creating the xml file with no namespace attributes.

Is XmlSerializer thread safe?

4 Answers. Show activity on this post. This type is thread safe.

How does the XmlSerializer work C#?

The XmlSerializer creates C# (. cs) files and compiles them into . dll files in the directory named by the TEMP environment variable; serialization occurs with those DLLs. These serialization assemblies can be generated in advance and signed by using the SGen.exe tool.


1 Answers

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);
}
like image 181
Achim Avatar answered Sep 28 '22 07:09

Achim