Xml serialization removes namespaces, declarations, and xml serialization
# Region serialization
/// <Summary>
/// Serialization
/// </Summary>
/// <Param name = "type"> type </param>
/// <Param name = "obj"> Object </param>
/// <Returns> </returns>
Public string Serializer (Type type, object obj)
{
MemoryStream Stream = new MemoryStream ();
XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();
Ns. Add ("", ""); // set the namespace to null, so that no namespace is available.
XmlSerializer xml = new XmlSerializer (type );
Try
{
// Serialize the object
Xml. Serialize (Stream, obj, ns );
}
Catch (InvalidOperationException)
{
Throw;
}
Stream. Position = 0;
StreamReader sr = new StreamReader (Stream );
String str = sr. ReadToEnd ();
Sr. Dispose ();
Stream. Dispose ();
Return str;
}
# Endregion
Public static string ObjectToXmlSerializer (Object Obj) {XmlWriterSettings settings = new XmlWriterSettings (); // remove the xml declaration settings. omitXmlDeclaration = true; settings. encoding = Encoding. default; System. IO. memoryStream mem = new MemoryStream (); using (XmlWriter writer = XmlWriter. create (mem, settings) {// remove the default namespace xmlns: xsd and xmlns: xsi XmlSerializerNamespaces ns = new XmlSerializerNamespaces (); ns. add ("", ""); XmlSerializer formatter = new XmlSerializer (Obj. getType (); formatter. serialize (writer, Obj, ns);} return Encoding. default. getString (mem. toArray ());}
Source URL: http://www.cnblogs.com/kissdodog/archive/2013/12/10/3468385.html