C#温故而知新学习系列之XML编程—XmlSerializer类把复杂对象序列化为XML文档(六)
前言
在上一文中我们讲了如何把对象序列化为XML文档,今天在这里我们讲一下如何把一个复杂对象序列化为XML文档。我们之前说过在创建一个实体类时,只要在该类的属性上加上自定义属性,XMLSerializer就可以读取这些属性,就可以将它们映射成XML元素和属性了。我们这里同样要用到这些原则
阅读目录
一:思路
二:实现步骤
三:运行效果
一:思路
我们先看一下下面这张图,这张图是一家物流公司的接口文档,我们来看看它们这个XML文档是怎么序列化出来的,首先我们分析这张图根节点是<RequestOrder/>节点,那么实体类中这个属性的标签一定是[XmlRoot]标签了,我们知道[XmlRoot]标签只能加在类上面所以,所以RequestOrder不能是属性只能是类,<RequestOrder/>节点内部除了有<logisticProviderID/>等同级的节点,还有<sender/>节点,而<sender/>节点内部又包含<name/>等其他子节点,所以<sender/>节点也是一个父节点,但是这个父节点是<RequestOrder/>的子节点,从这张图我们看到在这些节点都没有属性值,都是一个个元素。到这里我想我们就应该能根据以上的分析来创建这个作为映射的实体类了。
二:实现步骤
1:代码文件编写
映射的实体类
1.1 Order.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace MyXmlSerializer
{
//这个标签表示整个类都需要序列化
[Serializable()]
[XmlRoot]
public class RequestOrder
{
private Order _order;
[XmlElement]
public Order Order
{
get { return _order; }
set { _order = value; }
}
}
public class Order
{
//商务公司ID
private string _logisticProviderID;
[XmlElement]
public string logisticProviderID
{
get { return _logisticProviderID; }
set { _logisticProviderID = value; }
}
//客户标识
private string _customerId = "";
[XmlElement]
public string customerId
{
get { return _customerId; }
set { _customerId = value; }
}
//物流订单号
private string _txLogisticID = "";
[XmlElement]
public string txLogisticID
{
get { return _txLogisticID; }
set { _txLogisticID = value; }
}
//业务订单号
private string _tradeNo = "";
[XmlElement]
public string tradeNo
{
get { return _tradeNo; }
set { _tradeNo = value; }
}
//订单类型
private int _type;
[XmlElement]
public int type
{
get { return _type; }
set { _type = value; }
}
//订单标识
private int _flag;
[XmlElement]
public int flag
{
get { return _flag; }
set { _flag = value; }
}
//发送者
private sendinfo _send;
[XmlElement]
public sendinfo send
{
get { return _send; }
set { _send= value; }
}
}
public class sendinfo
{
//姓名
private string _name;
[XmlElement]
public string name
{
get { return _name; }
set { _name = value; }
}
//邮政编码
private string _postCode;
[XmlElement]
public string postCode
{
get { return _postCode; }
set { _postCode = value; }
}
//电话
private string _phone;
[XmlElement]
public string phone
{
get { return _phone; }
set { _phone = value; }
}
//手机
private string _mobile;
[XmlElement]
public string mobile
{
get { return _mobile; }
set { _mobile = value; }
}
//省
private string _prov;
[XmlElement]
public string prov
{
get { return _prov; }
set { _prov = value; }
}
//城市
private string _city;
[XmlElement]
public string city
{
get { return _city; }
set { _city = value; }
}
//地址
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>
/// 把XML文档反序列化为对象
/// </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 = "张三";
requestorder.Order.send.postCode = "310013";
requestorder.Order.send.phone = "231234134";
requestorder.Order.send.mobile = "13575745195";
requestorder.Order.send.prov = "浙江省";
requestorder.Order.send.city = "杭州市,西湖区";
requestorder.Order.send.address = ">华星科技大厦9层";
XmlSerializer xmlserializer = new XmlSerializer(typeof(RequestOrder));
TextWriter textwriter = new StreamWriter("RequestOrder.xml");
xmlserializer.Serialize(textwriter, requestorder);
textwriter.Close();
}
}
}
三:运行效果
当我们单击“把复杂对象序列化为XML文档”按钮后,就会看到如下图生成的RequestOrder.xml文档,打开这个文档我们看到是不是和物流公司的接口文档一致呢。