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

Source: Internet
Author: User

  Preface
In the previous article, we talked about how to serialize an object into an XML document. Today we will talk about how to serialize a complex object into an XML document. As we have mentioned before, when creating an object class, XMLSerializer can read these attributes by adding custom attributes to the class attributes and map them to XML elements and attributes. These principles are also used here.

  Reading directory

  I. Ideas

II. Implementation steps

Iii. Running Effect
I:Ideas
Let's first take a look at the figure below. This figure is an interface document of a logistics company. Let's take a look at how these XML documents are serialized, first, we will analyze that the root node of this graph is a <RequestOrder/> node, so the label of this attribute in the object class must be a [XmlRoot] label, we know that the [XmlRoot] label can only be added to the class. Therefore, the RequestOrder cannot be an attribute but a class. The <RequestOrder/> node contains only nodes with the same level as <logisticProviderID/>, there are also <sender/> nodes, and the <sender/> node contains <name/> and other sub-nodes. Therefore, the <sender/> node is also a parent node, however, this parent node is a child node of <RequestOrder/>. From this figure, we can see that there are no attribute values for these nodes, and they are all elements. Here I think we should be able to create this ing entity Class Based on the above analysis.

  

  II. Implementation steps

1: code file writing

Ing object class

1.1 Order. 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 RequestOrder
{
Private Order _ order;
[XmlElement]
Public Order
{
Get {return _ order ;}
Set {_ order = value ;}
}
}

Public class Order
{
// Business company ID
Private string _ logisticProviderID;
[XmlElement]
Public string logisticProviderID
{
Get {return _ logisticProviderID ;}
Set {_ logisticProviderID = value ;}
}
// Customer ID
Private string _ customerId = "";
[XmlElement]
Public string customerId
{
Get {return _ customerId ;}
Set {_ customerId = value ;}
}
// Logistics order number
Private string _ txLogisticID = "";
[XmlElement]
Public string txLogisticID
{
Get {return _ txLogisticID ;}
Set {_ txLogisticID = value ;}
}
// Business order number
Private string _ tradeNo = "";
[XmlElement]
Public string tradeNo
{
Get {return _ tradeNo ;}
Set {_ tradeNo = value ;}
}
// Order type
Private int _ type;
[XmlElement]
Public int type
{
Get {return _ type ;}
Set {_ type = value ;}
}
// Order ID
Private int _ flag;
[XmlElement]
Public int flag
{
Get {return _ flag ;}
Set {_ flag = value ;}
}
// Sender
Private sendinfo _ send;
[XmlElement]
Public sendinfo send
{
Get {return _ send ;}
Set {_ send = value ;}
}

}

Public class sendinfo
{
// Name
Private string _ name;
[XmlElement]
Public string name
{
Get {return _ name ;}
Set {_ name = value ;}
}
// Zip code
Private string _ postCode;
[XmlElement]
Public string postCode
{
Get {return _ postCode ;}
Set {_ postCode = value ;}
}
// Call
Private string _ phone;
[XmlElement]
Public string phone
{
Get {return _ phone ;}
Set {_ phone = value ;}
}
// Mobile phone
Private string _ mobile;
[XmlElement]
Public string mobile
{
Get {return _ mobile ;}
Set {_ mobile = value ;}
}
// Save
Private string _ prov;
[XmlElement]
Public string prov
{
Get {return _ prov ;}
Set {_ prov = value ;}
}
// City
Private string _ city;
[XmlElement]
Public string city
{
Get {return _ city ;}
Set {_ city = value ;}
}
// Address
Private string _ address;
[XmlElement]
Public string address
{
Get {return _ address ;}
Set {_ address = value ;}
}
}
}

1.2 Form2.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 Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}

/// <Summary>
/// Deserialize an XML document as an object
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void btnSerialize_Click (object sender, EventArgs e)
{
RequestOrder requestorder = new RequestOrder ();
Requestorder. Order = new Order ();
Requestorder. Order. logisticProviderID = "TB ";
Requestorder. Order. customerId = "a92266073246b3ed2a2f0ff4d0b2bf5e ";
Requestorder. Order. txLogisticID = "LP07082300225709 ";
Requestorder. Order. tradeNo = "2007082300225709 ";
Requestorder. Order. type = 0;
Requestorder. Order. flag = 1;
Requestorder. Order. send = new sendinfo ();
Requestorder. Order. send. name = "Zhang San ";
Requestorder. Order. send. postCode = "310013 ";
Requestorder. Order. send. phone = "231234134 ";
Requestorder. Order. send. mobile = "13575745195 ";
Requestorder. Order. send. prov = "Zhejiang Province ";
Requestorder. Order. send. city = "Hangzhou, Xihu District ";
Requestorder. Order. send. address = "> 9 Floor, Huaxing Technology Building ";
XmlSerializer xmlserializer = new XmlSerializer (typeof (RequestOrder ));
TextWriter textwriter = new StreamWriter ("RequestOrder. xml ");
Xmlserializer. Serialize (textwriter, requestorder );
Textwriter. Close ();
}
}
}

  Iii. Running Effect

After you click "serialize a complex object to an XML document", the generated RequestOrder is displayed. open this document to see if it is consistent with the logistics company's interface 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.