我有以下架构(部分内容已发布):
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="RESPONSE">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="SERVICE"/>
<xsd:element ref="ACTION"/>
<xsd:element ref="STATUS"/>
<xsd:element ref="HEADER"/>
<xsd:element ref="MESSAGE"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="SERVICE" type="xsd:string"/>
<xsd:element name="ACTION" type="xsd:string"/>
<xsd:element name="STATUS" type="xsd:string"/>
<xsd:element name="MESSAGE">
<xsd:complexType>
<xsd:sequence>
<xsd:choice>
<xsd:element ref="ITEM" maxOccurs="unbounded"/>
<xsd:element ref="PARSER"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="HEADER">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="MEMBER_ID"/>
<xsd:element ref="USER_ID"/>
<xsd:element ref="RUN_NO"/>
<xsd:element ref="TOT_ITEMS"/>
<xsd:element ref="ERR_ITEMS"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ITEM">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="ENQUIRY_REFERENCE" minOccurs="0"/>
<xsd:choice>
<xsd:element ref="RSP_REPORT"/>
<xsd:sequence>
<xsd:element ref="ERROR" maxOccurs="unbounded"/>
<xsd:element ref="NO_ERRORS"/>
</xsd:sequence>
</xsd:choice>
</xsd:sequence>
<xsd:attribute name="seq" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="MEMBER_ID" type="xsd:string"/>
<xsd:element name="USER_ID" type="xsd:string"/>
<xsd:element name="RUN_NO" type="xsd:string"/>
<xsd:element name="TOT_ITEMS" type="xsd:string"/>
<xsd:element name="ERR_ITEMS" type="xsd:string"/>
<xsd:element name="RSP_MSG" type="xsd:string"/>
<xsd:element name="CONSUMER_SEQ" type="xsd:string"/>
<xsd:element name="FIELD" type="xsd:string"/>
<xsd:element name="DATA" type="xsd:string"/>
<xsd:element name="NO_ERRORS" type="xsd:string"/>
</xsd:schema>
现在,我使用XSD2Code生成以下类(仅显示其中的一部分):
namespace EnquiryRsp
{
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.Collections.Generic;
public partial class RESPONSE
{
private string sERVICEField;
private string aCTIONField;
private string sTATUSField;
private HEADER hEADERField;
private MESSAGE mESSAGEField;
public RESPONSE()
{
this.mESSAGEField = new MESSAGE();
this.hEADERField = new HEADER();
}
public string SERVICE
{
get
{
return this.sERVICEField;
}
set
{
this.sERVICEField = value;
}
}
public string ACTION
{
get
{
return this.aCTIONField;
}
set
{
this.aCTIONField = value;
}
}
public string STATUS
{
get
{
return this.sTATUSField;
}
set
{
this.sTATUSField = value;
}
}
public HEADER HEADER
{
get
{
return this.hEADERField;
}
set
{
this.hEADERField = value;
}
}
public MESSAGE MESSAGE
{
get
{
return this.mESSAGEField;
}
set
{
this.mESSAGEField = value;
}
}
}
public partial class HEADER
{
private string mEMBER_IDField;
private string uSER_IDField;
private string rUN_NOField;
private string tOT_ITEMSField;
private string eRR_ITEMSField;
public string MEMBER_ID
{
get
{
return this.mEMBER_IDField;
}
set
{
this.mEMBER_IDField = value;
}
}
public string USER_ID
{
get
{
return this.uSER_IDField;
}
set
{
this.uSER_IDField = value;
}
}
public string RUN_NO
{
get
{
return this.rUN_NOField;
}
set
{
this.rUN_NOField = value;
}
}
public string TOT_ITEMS
{
get
{
return this.tOT_ITEMSField;
}
set
{
this.tOT_ITEMSField = value;
}
}
public string ERR_ITEMS
{
get
{
return this.eRR_ITEMSField;
}
set
{
this.eRR_ITEMSField = value;
}
}
}
public partial class MESSAGE
{
private List<object> itemsField;
public MESSAGE()
{
this.itemsField = new List<object>();
}
public List<object> Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
public partial class ITEM
{
private string eNQUIRY_REFERENCEField;
private List<object> itemsField;
private string seqField;
public ITEM()
{
this.itemsField = new List<object>();
}
public string ENQUIRY_REFERENCE
{
get
{
return this.eNQUIRY_REFERENCEField;
}
set
{
this.eNQUIRY_REFERENCEField = value;
}
}
public List<object> Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
public string seq
{
get
{
return this.seqField;
}
set
{
this.seqField = value;
}
}
}
}
以下是我试图反序列化的XML:
<RESPONSE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="EnquiryRsp.xsd">
<SERVICE>ENQUIRY</SERVICE>
<ACTION>A_SC</ACTION>
<STATUS>OK</STATUS>
<HEADER>
<MEMBER_ID>XXX</MEMBER_ID>
<USER_ID>XXXXX</USER_ID>
<RUN_NO>1</RUN_NO>
<TOT_ITEMS>1</TOT_ITEMS>
<ERR_ITEMS/>
</HEADER>
<MESSAGE>
<ITEM>
<ENQUIRY_REFERENCE>FOS000279511111UKU</ENQUIRY_REFERENCE>
<RSP_REPORT>
<ENQUIRY_TYPE>RV</ENQUIRY_TYPE>
<REPORT_DATE>26/12/2007</REPORT_DATE>
<ENQUIRY_NO>17248697</ENQUIRY_NO>
<PRODUCT_TYPE>PLN</PRODUCT_TYPE>
<NO_OF_APPLICANTS>1</NO_OF_APPLICANTS>
<ACCOUNT_TYPE>S</ACCOUNT_TYPE>
<ENQUIRY_REFERENCE>FOS000279511111UKU</ENQUIRY_REFERENCE>
<AMOUNT>0.00</AMOUNT>
<MBR_TYPE>FULL</MBR_TYPE>
<MBR_STS>ACTIV</MBR_STS>
<CONSUMER>
</CONSUMER>
<DISCLAIMER>
<DI_TEXT></DI_TEXT>
<DI_TEXT_AR lang="ar"></DI_TEXT_AR>
</DISCLAIMER>
</RSP_REPORT>
</ITEM>
</MESSAGE>
</RESPONSE>
我无法在消息中反序列化ITEM列表。当我使用XmlSerializer.Deserialize方法反序列化XML时,它不会失败,但会将Message.Items列表保留为空。有什么建议吗?
答案 0 :(得分:1)
尝试使用
[XmlRoot( “RESPONSE”)] 公共部分类响应
[XmlElement的( “服务”)] 公共字符串服务
示例
[XmlRoot("Context")]
public class Context
{
public Context() { this.Persons = new Persons(); }
[XmlArray("Persons")]
[XmlArrayItem("Person")]
public Persons Persons { get; set; }
}
public class Persons : List<Human> { }
public class Human
{
public Human() { }
public Human(string name) { Name = name; }
public string Name { get; set; }
}
class Program
{
public static void Main(string[] args)
{
Context ctx = new Context();
ctx.Persons.Add(new Human("john"));
ctx.Persons.Add(new Human("jane"));
var writer = new StringWriter();
new XmlSerializer(typeof(Context)).Serialize(writer, ctx);
Console.WriteLine(writer.ToString());
}
}