C # XML programming for the newest Learning Series-XmlSerializer class serializes objects into XML documents (5)

Source: Internet
Author: User

  Preface
The XMLSerializer class contains methods for serializing an object into an XML document and deserializing an XML document as an object. When creating a class, you only need to add custom attributes to the class attributes, XMLSerializer can read these attributes and map them to XML elements and attributes.

In the XMLSerializer class, the object is serialized as an XML document and implemented using the Serialize () method. deserialization is implemented using the Deserialize () method.

To serialize a class, you can add the [Serializable] label before it, that is, add a Serializable attribute to the class, and the [NonSerializable] label also exists, it is only applicable to fields in the class defined by the [Serializable] label. It indicates that this field does not need to be serialized.

Let me take a bookstore example to serialize an object into an XML document of bookstore.

 Reading directory

1. Implementation steps

  Ii. Running Effect

 Instance

1. Implementation steps

1: code file writing

1.1 BookShop. cs

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Xml;
Using System. Xml. Serialization;

Namespace MyXmlSerializer
{
// This label indicates that the entire class needs to be serialized.
[Serializable ()]
[XmlRoot]
Public class BookShop
{
Private Book _ book;
[XmlElement]
Public Book
{
Get {return _ book ;}
Set {_ book = value ;}
}
}

Public class Book
{
String _ booknumber = "";
[XmlAttribute]
Public string BookNumber
{
Get {return _ booknumber ;}
Set {_ booknumber = value ;}
}

String _ name = "";
[XmlElement]
Public string Name
{
Get {return _ name ;}
Set {_ name = value ;}
}

String _ othorname = "";
[XmlElement]
Public string OthorName
{
Get {return _ othorname ;}
Set {_ othorname = value ;}
}

String _ author = "";
[XmlElement]
Public string Author
{
Get {return _ author ;}
Set {_ author = value ;}
}

String _ money = "";
[XmlElement]
Public string Money
{
Get {return _ money ;}
Set {_ money = value ;}
}

String _ issu = "";
[XmlElement]
Public string Issu
{
Get {return _ issu ;}
Set {_ issu = value ;}
}
}
}

1.2 Form1.cs

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Xml;
Using System. Xml. Serialization;
Using System. IO;

Namespace MyXmlSerializer
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

/// <Summary>
/// Serialize an object into an XML document
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void btnSerialize_Click (object sender, EventArgs e)
{
BookShop bookshhop = new BookShop ();
Bookshhop. Book = new Book ();
Bookshhop. Book. BookNumber = "001 ";
Bookshhop. Book. Name = "Journey to the West";
Bookshhop. Book. OthorName = "Datang xi you Chuan ";
Bookshhop. Book. Author = "Wu chengen ";
Bookshhop. Book. Money = "320 ";
Bookshhop. Book. Issu = "Tsinghua University Press ";
XmlSerializer xmlserializer = new XmlSerializer (typeof (BookShop ));
TextWriter textwriter = new StreamWriter ("BookShop. xml ");
Xmlserializer. Serialize (textwriter, bookshhop );
Textwriter. Close ();
}

/// <Summary>
/// Serialize an object set into an XML document
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void button#click (object sender, EventArgs e)
{
List <BookShop> bookshhop_list = new List <BookShop> ();
For (int I = 0; I <3; I ++)
{
BookShop bookshhop = new BookShop ();
Bookshhop. Book = new Book ();
// Here it should be obtained from the database. The object attribute values are different. to simplify it, I directly assign the same values.
Bookshhop. Book. BookNumber = "001 ";
Bookshhop. Book. Name = "Journey to the West";
Bookshhop. Book. OthorName = "Datang xi you Chuan ";
Bookshhop. Book. Author = "Wu chengen ";
Bookshhop. Book. Money = "320 ";
Bookshhop. Book. Issu = "Tsinghua University Press ";
Bookshhop_list.Add (bookshhop );
}
XmlSerializer xmlserializer = new XmlSerializer (typeof (List <BookShop> ));
TextWriter textwriter = new StreamWriter ("BookShopList. xml ");
Xmlserializer. Serialize (textwriter, bookshhop_list );
Textwriter. Close ();
}
}
}

  Ii. Running Effect

After you click the serialize object to XML document button, a BookShop is generated. xml file. The following figure shows the effect of Opening this file. This is to serialize an object into an XML document. When we click "serialize an object set to an XML document", the effect is as follows, the object set is serialized as an XML document.

  

  

  

  

 

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.