Introducing XML serialization

Source: Internet
Author: User

The following items can be serialized usingXmlserializerClass:

    • Public read/write properties and fields of public classes

    • Classes that implementIcollectionOrIenumerable

      Note

      Only collections are serialized, not public properties.

    • XmlelementObjects

    • XmlnodeObjects

    • DatasetObjects

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.