The following items can be serialized usingXmlserializerClass:
Example code for icollection:
Using System; Using System. IO; Using System. collections; Using System. xml. serialization; Public Class Test { Static Void Main () {test T = New Test (); T. serializecollection ( " Coll. xml " );} Private Void Serializecollection ( String Filename) {employees EMPs = New Employees (); // Note that only the collection is serialized -- not // Collectionname or any other public property of the class. EMPs. collectionname = " Employees " ; Employee john100 = New Employee ( " John " , " 100xxx " ); EMPs. Add (john100); xmlserializer x = New Xmlserializer ( Typeof (Employees); textwriter writer = New Streamwriter (filename); X. serialize (writer, EMPs );}} Public Class Employees: icollection { Public String Collectionname; Private Arraylist emparray = New Arraylist (); Public Employee This [ Int Index] { Get { Return (Employee) emparray [Index];} Public Void Copyto (array, Int Index) {emparray. copyto (A, index );} Public Int Count { Get { Return Emparray. Count ;}} Public Object Syncroot { Get { Return This ;}} Public Bool Issynchronized { Get { Return False ;}} Public Ienumerator getenumerator (){ Return Emparray. getenumerator ();} Public Void Add (employee newemployee) {emparray. Add (newemployee );}} Public Class Employee { Public String Empname; Public String Empid; Public Employee (){} Public Employee ( String Empname, String Empid) {empname = Empname; empid = Empid ;}}
The serialization XML looks like:
<? XML version = "1.0" encoding = "UTF-8" ?> < Arrayofemployee Xmlns: xsi = "Http://www.w3.org/2001/XMLSchema-instance" Xmlns: XSD = "Http://www.w3.org/2001/XMLSchema" > < Employee > < Empname > John </ Empname > < Empid > 100xxx </ Empid > </ Employee > </ Arrayofemployee >
Dataset XML serialization example:
Private Void Serializedataset ( String Filename) {xmlserializer Ser = New Xmlserializer (Typeof (Dataset )); // Creates a dataset; adds a table, column, and ten rows. Dataset DS = New Dataset ( " Mydataset " ); Datatable t = New Datatable ( " Table1 " ); Datacolumn C =New Datacolumn ( " Thing " ); T. Columns. Add (c); DS. Tables. Add (t); datarow R; For ( Int I = 0 ; I < 10 ; I ++ ) {R = T. newrow (); R [ 0 ] = " Thing " + I; T. Rows. Add (r);} textwriter writer = New Streamwriter (filename); Ser. serialize (writer, DS); writer. Close ();}
Xmlelement or xmlnode XML serialization code example:
Private Void Serializeelement ( String Filename) {xmlserializer Ser = New Xmlserializer ( Typeof (Xmlelement); xmlelement myelement =New Xmldocument (). createelement ( " Myelement " , " NS " ); Myelement. innertext = " Hello World " ; Textwriter writer = New Streamwriter (filename); Ser. serialize (writer, myelement); writer. Close ();} Private Void Serializenode ( String Filename) {xmlserializer Ser = New Xmlserializer ( Typeof (Xmlnode); xmlnode mynode = New Xmldocument (). createnode (xmlnodetype. element, " Mynode " , " NS " ); Mynode. innertext = " Hello Node " ; Textwriter writer = New Streamwriter (filename); Ser. serialize (writer, mynode); writer. Close ();}
You also can control the XML serialization by attribute. And the attributes which can control the XML serialization.