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