C#实体类生成XML(注意<![CDATA]>标签的不解析)
最近跟人对接,使用的是WebService,之前对接用WebService也就是直接传参数就行,像调方法那样简单,这次。。。要我按XML传,为了搞他那个格式,花了我点时间。。。特此记录。
直接贴代码和生成的XML了要注意的是这里的
<BusinessRequest>标签,这个
<![CDATA]>;里的内容他是不解析的,所以在xml生成实体类的时候要写如下代码:
[XmlElement("BusinessRequest")] public XmlNode[] Nodes { get { var dom = new XmlDocument(); return new XmlNode[] { dom.CreateCDataSection(Content.Text) }; } set { if (value == null) { Content = new BusinessRequest(); return; } } }
<ESBEnvelope xmlns="http://ESB.TopSchemaV2"> <ESBHeader><HeaderControl AppCode="123" Password="123" MessageCategory="123" Version="1" CreateTime="2021/6/3 11:03:12" /></ESBHeader> <ESBBody> <BodyControl CallType=""/>
<BusinessRequest><![CDATA[<QueryParameter><DeptCode>03060101</DeptCode></QueryParameter>]]></BusinessRequest>
</ESBBody> </ESBEnvelope>
实体类代码:
[XmlRoot("ESBEnvelope", Namespace = "http://ESB.TopSchemaV2")] public class ESBEnvelope { [XmlElement] public ESBHeader ESBHeader { get; set; } = new ESBHeader(); [XmlElement] public ESBBody ESBBody { get; set; } = new ESBBody(); } public class ESBHeader { [XmlElement] public HeaderControl HeaderControl { get; set; } = new HeaderControl(); } public class HeaderControl { [XmlAttribute(AttributeName = "AppCode")] public string AppCode { get; set; } = "123"; [XmlAttribute(AttributeName = "Password")] public string Password { get; set; } = "123"; [XmlAttribute(AttributeName = "MessageCategory")] public string MessageCategory { get; set; } = "123"; [XmlAttribute(AttributeName = "Version")] public string Version { get; set; } = "1"; [XmlAttribute(AttributeName = "CreateTime")] public string CreateTime { get; set; } = DateTime.Now.ToString(); [XmlAttribute(AttributeName = "MyProperty")] public string MyProperty { get; set; } } public class ESBBody { [XmlElement] public BodyControl BodyControl { get; set; } = new BodyControl(); [XmlIgnore] public BusinessRequest Content { get; set; } = new BusinessRequest(); [XmlElement("BusinessRequest")] public XmlNode[] Nodes { get { var dom = new XmlDocument(); return new XmlNode[] { dom.CreateCDataSection(Content.Text) }; } set { if (value == null) { Content = new BusinessRequest(); return; } } } } public class BodyControl { [XmlAttribute(AttributeName = "CallType")] public string CallType { get; set; } = ""; } public class BusinessRequest { [XmlIgnore] public string Text { get; set; } = "<QueryParameter><DeptCode>03060101</DeptCode></QueryParameter>"; } public class QueryPTSDrugCustodyInventoryService { [XmlElement] public List<Demography> Demography { get; set; } } public class Demography { /// <summary> /// 患者ID /// </summary> public string PatientID { get; set; } /// <summary> /// 病历号 /// </summary> public string MedicalRecordNo { get; set; } /// <summary> /// 身份证号 /// </summary> public string SSN { get; set; } /// <summary> /// 患者姓名 /// </summary> public string PatientName { get; set; } /// <summary> /// 性别 /// </summary> public string Sex { get; set; } /// <summary> /// 出生日期 /// </summary> public string Birthday { get; set; } /// <summary> /// 婚姻状况 /// </summary> public string MaritalStatus { get; set; } /// <summary> /// 联系电话 /// </summary> public string LinkTel { get; set; } /// <summary> /// 药品信息 /// </summary> [XmlElement] public List<DrugInfo> DrugInfo { get; set; } } public class DrugInfo { public string DrugCode { get; set; } /// <summary> /// 药品名称 /// </summary> public string DrugName { get; set; } /// <summary> /// 规格 /// </summary> public string Specifications { get; set; } /// <summary> /// 生产厂商 /// </summary> public string Manufacturer { get; set; } /// <summary> /// 用药总数量 /// </summary> public string TotalDosage { get; set; } /// <summary> /// 总数单位 /// </summary> public string TotalUnit { get; set; } /// <summary> /// 剩余数量 /// </summary> public string TotalSurplus { get; set; } /// <summary> /// 剩余单位 /// </summary> public string SurplusUnit { get; set; } }