Http://www.codeproject.com/KB/XML/Serialization_Samples.aspx
If the class implements idictionary, it cannot be serialized. For example, HashTables cannot be serialized.
By using XML serialization only public properties and fields can be serialized. If Private Members are to be serialized, other serialization Methods shocould be used.
It requiresParameterlessConstructor. This constructor is called during deserialization.
Serialized form of the object does not contain any type, assembly information of the class. Only the data is stored.
Using XML serialization not all the classes can be serialized. classes that implementIDictionary
Cannot be serialized. E. g.Hashtable
S cannot be serialized. arrays of objects can be easily serialized. Collections can be serialized but to serialize collections certain rules must be followed.
NULL values are not serialized with XML serialization. To include the null members in the serialized formIsNullable
Attribute shocould be settrue
. For example:
[Xmlelement (isnullable = true)]
Remove xmlns:
Xmlserializernamespaces namespaces = new xmlserializernamespaces ();
Namespaces. Add (string. Empty, String. Empty );
Serializer. serialize (writer, source, namespaces );
Change the root element name: rootname Xmlrootattribute root = new xmlrootattribute ();
Root. elementname = Name;
Xmlserializer xs = new xmlserializer (typeof (t), root );
Format attributes of Nested classes in a serialization class:
[Xmlarrayitem (type = typeof (netatt), elementname = "item")]
Public list <netatt> netatts;
You can also customize xmltextwriter to remove XML declaration. <? XML version = "1.0" encoding = "UTF-8"?>
Public class specialxmlwriter: xmltextwriter
{
Bool m_includestartdocument = true;
Public specialxmlwriter (textwriter TW, bool includestartdocument)
: Base (TW)
{
M_includestartdocument = includestartdocument;
}
Public specialxmlwriter (Stream SW, encoding, bool includestartdocument)
: Base (SW, null)
{
M_includestartdocument = includestartdocument;
}
Public specialxmlwriter (string filepath, encoding, bool includestartdocument)
: Base (filepath, null)
{
M_includestartdocument = includestartdocument;
}
Public override void writestartdocument ()
{
If (m_includestartdocument)
{
Base. writestartdocument ();
}
}
}
The test is as follows:
Xmlserializer serializer = NULL;
Specialxmlwriter swriter = NULL;
Try
{
Serializer = new xmlserializer (objxml. GetType ());
Swriter = new specialxmlwriter (server. mappath ("~ /A. xml "), system. Text. encoding. Default, false );
If (! Isnamespaces)
{
Xmlserializernamespaces xnamesspace = new xmlserializernamespaces ();
Xnamesspace. Add ("","");
Serializer. serialize (swriter, objxml, xnamesspace );
}
Else
Serializer. serialize (swriter, objxml );
}
Catch (exception ex)
{
Throw new exception (ex. tostring ());
}
Finally
{
If (swriter! = NULL)
{
Swriter. Close ();
}
}
// Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. xml. serialization;
Using system. IO;
Using system. runtime. serialization;
Using system. xml;
Public partial class default2: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
}
Protected void button#click (Object sender, eventargs E)
{
List <garyperson> objlist = new list <garyperson> (){
New garyperson {strpassword = "James", strusername = "p "}
};
Serialize (objlist, server. mappath ("~ /Obj. xml "));
}
Void serialize (Object source, string file)
{
Xmlwritersettings settings = new xmlwritersettings ();
Settings. omitxmldeclaration = true;
Settings. indent = true;
Using (xmlwriter writer = xmlwriter. Create (file, settings ))
{
Xmlrootattribute root = new xmlrootattribute ("garylist ");
Xmlserializer serializer = new xmlserializer (source. GetType (), root );
Xmlserializernamespaces namespaces = new xmlserializernamespaces ();
Namespaces. Add (string. Empty, String. Empty );
Serializer. serialize (writer, source, namespaces );
}
}
}
[Serializable]
Public class garyperson
{
Public String strusername;
Public String strpassword;
}