动态生成XML

前端之家收集整理的这篇文章主要介绍了动态生成XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、新建EmailInfo.cs页面,成发布xml的类定义

/// <summary>
/// 用于生成发布xml的类定义
/// </summary>
[XmlRootAttribute("EmailInfoRoot",Namespace = "",IsNullable = false)]
public class EmailInfo
{
[XmlArrayItem("EmailInfo")]
public EmailMain[] emMailInfo;
[XmlArray("EmailTos")]
[XmlArrayItem("EmailTo")]
public EmailTo[] etToInfo;
[XmlArray("EmailAttachments")]
[XmlArrayItem("EmailAttachment")]
public EmailAttachment[] eaAttachmentInfo;
}

[XmlRootAttribute("EmailTo",IsNullable = false)]
public class EmailTo
{
/// <summary>
/// WorkflowID
/// </summary>
[Description("WorkflowID")]
public string WorkflowID { get; set; }

}

[XmlRootAttribute("EmailAttachment",IsNullable = false)]
public class EmailAttachment
{
/// <summary>
/// WorkflowID
/// </summary>
[Description("WorkflowID")]
public string WorkflowID { get; set; }

}

[XmlRootAttribute("EmailMain",IsNullable = false)]
public class EmailMain
{
/// <summary>
/// WorkflowID
/// </summary>
[Description("WorkflowID")]
public string WorkflowID { get; set; }

}

-------------------------------------------------------------------------------------------------------------

二、引用类

string strXMLData = ClassSerializer.ToString<Model.EmailInfo>(listEmailInfo[0]);

--------------------------------------------------------------------------------------------------------------

三、生成xml

public static string ToString<T>(T vClassEntity)
{
XmlSerializer x = new XmlSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
xmlns.Add("","");
x.Serialize(ms,vClassEntity,xmlns);
ms.Close();
string strReturn = Encoding.UTF8.GetString(ms.ToArray());

return strReturn;
}



猜你在找的XML相关文章