发布:2019/12/1 16:43:16作者:管理员 来源:本站 浏览次数:1122
第一步,先将类设置为可序列化xml
如下所示
/// <summary>
/// 代理商
/// </summary>
[XmlRoot(ElementName = "Agent")]
public class YyhAgent
{
[XmlElement("AgentId")]
public string AgentId { get; set; }
[XmlElement("AgentName")]
public string AgentName { get; set; }
}
/// <summary>
/// 返回的代理商列表格式
/// </summary>
[XmlRoot("Root")]
public class YyhAgents
{
[XmlElement(ElementName = "ManufacturerId")]
public string ManufacturerId { get; set; }
[XmlElement(ElementName = "RecCount")]
public string RecCount { get; set; }
[XmlElement(ElementName = "PageSize")]
public string PageSize { get; set; }
[XmlElement(ElementName = "PageIndex")]
public string PageIndex { get; set; }
[XmlElement(typeof(List<YyhAgent>))]
[XmlElement("AgentList")]
public List<YyhAgent> lst { get; set; }
}
第二步,调用如下方法即可
public static XmlDataDocument SerialiazerXmlDataDoc(Type oType, object oData)
{
XmlSerializer oXmlSerializer = new XmlSerializer(oType); //为序列化器指定序列化类型
MemoryStream oMemStream = new MemoryStream();
oXmlSerializer.Serialize(oMemStream, oData); //为序列化器指定要被序列化的数据
XmlDataDocument oXmlDataDoc = new XmlDataDocument();
oXmlDataDoc.LoadXml(System.Text.Encoding.UTF8.GetString(oMemStream.ToArray()));
//将序列化的结果,转换为字符串加载进xmldatadocument对象
return oXmlDataDoc;
}
————————————————
版权声明:本文为CSDN博主「一剑平江湖」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/diligentcatrich/article/details/6854468