My journey to WCF (4): serialization in WCF [Part II]

Source: Internet
Author: User

XMLSerializer

When it comes to XMLSerializer, I think most people know that this is the Serializer used by asmx. First, let's look at an example. By comparing the structure of the Managed Type and the generated XML structure, we can summarize what kind of ing method is used for serialization. Like DataContractSerialzer Sample, we need to define the Type -- XMLOrder and XMLProduct to which the serialization object belongs. They have the same members as the corresponding DataContractOrder and DataContractProduct.


Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace Artech. WCFSerialization
{
Public class XMLProduct
{
Private Fields # region Private Fields
Private Guid _ productID;
Private string _ productName;
Private string _ producingArea;
Private double _ unitPrice;

Constructors # region Constructors
Public XMLProduct ()
{
Console. WriteLine ("The constructor of XMLProduct has been invocated! ");
}

Public XMLProduct (Guid id, string name, string producingArea, double price)
{
This. _ productID = id;
This. _ productName = name;
This. _ producingArea = producingArea;
This. _ unitPrice = price;
}

# Endregion

Properties # region Properties
Public Guid ProductID
{
Get {return _ productID ;}
Set {_ productID = value ;}
}

Public string ProductName
{
Get {return _ productName ;}
Set {_ productName = value ;}
}

Internal string ProducingArea
{
Get {return _ producingArea ;}
Set {_ producingArea = value ;}
}

Public double UnitPrice
{
Get {return _ unitPrice ;}
Set {_ unitPrice = value ;}
}

# Endregion

}
}


Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace Artech. WCFSerialization
{
Public class XMLOrder
{
Private Guid _ orderID;
Private DateTime _ orderDate;
Private XMLProduct _ product;
Private int _ quantity;

Constructors # region Constructors
Public XMLOrder ()
{
This. _ orderID = new Guid ();
This. _ orderDate = DateTime. MinValue;
This. _ quantity = int. MinValue;

Console. WriteLine ("The constructor of XMLOrder has been invocated! ");
}

Public XMLOrder (Guid id, DateTime date, XMLProduct product, int quantity)
{
This. _ orderID = id;
This. _ orderDate = date;
This. _ product = product;
This. _ quantity = quantity;
}
# Endregion

Properties # region Properties
Public Guid OrderID
{
Get {return _ orderID ;}
Set {_ orderID = value ;}
}

Public DateTime OrderDate
{
Get {return _ orderDate ;}
Set {_ orderDate = value ;}
}

Public XMLProduct Product
{
Get {return _ product ;}
Set {_ product = value ;}
}

Public int Quantity
{
Get {return _ quantity ;}
Set {_ quantity = value ;}
}
# Endregion

Public override string ToString ()
{
Return string. format ("ID: {0} Date: {1} Product: ID: {2} Name: {3} Producing Area: {4} Price: {5} Quantity: {6 }",
This. _ orderID, this. _ orderDate, this. _ product. productID, this. _ product. productName, this. _ product. producingArea, this. _ product. unitPrice, this. _ quantity );
}
}
}

Compile the Serialization Code.

 

Static void SerializeViaXMLSerializer ()
{
XMLProduct produc

Related Article

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.