Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the xmlserializer only serialize plain xml?

I need to get plain xml, without the <?xml version="1.0" encoding="utf-16"?> at the beginning and xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" in first element from XmlSerializer. How can I do it?

like image 231
Grzenio Avatar asked Nov 20 '09 17:11

Grzenio


People also ask

What is the correct way of using XML serialization?

XML Serialization Considerations Type identity and assembly information are not included. Only public properties and fields can be serialized. Properties must have public accessors (get and set methods). If you must serialize non-public data, use the DataContractSerializer class rather than XML serialization.

What is the difference between binary serialization and XML serialization?

Xml Serializer serializes only public member of object but Binary Serializer serializes all member whether public or private. In Xml Serialization, some of object state is only saved but in Binary Serialization, entire object state is saved.

What does it mean to serialize XML?

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.

What is System XML serialization XmlElementAttribute?

The XmlElementAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object. For a complete list of similar attributes, see Attributes That Control XML Serialization.


1 Answers

To put this all together - this works perfectly for me:

    // To Clean XML     public string SerializeToString<T>(T value)     {         var emptyNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });         var serializer = new XmlSerializer(value.GetType());         var settings = new XmlWriterSettings();         settings.Indent = true;         settings.OmitXmlDeclaration = true;          using (var stream = new StringWriter())         using (var writer = XmlWriter.Create(stream, settings))         {             serializer.Serialize(writer, value, emptyNamespaces);             return stream.ToString();         }     } 
like image 150
Simon Sanderson Avatar answered Sep 17 '22 14:09

Simon Sanderson