我遇到了序列化加倍表示“caseList”类的xml属性的问题:
这是我得到的:
<import xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance" xsi_noNamespaceSchemaLocation="TR-DGU%20Import-Schema%20V2015%20-%20Stand%20M%C3%A4rz%202016.xsd">
<creationDate>2016-05-19</creationDate>
<hospitalCode>A-0000-A</hospitalCode>
<importCasesWithErrors>1</importCasesWithErrors>
<caseList>
<case>
<patientCode>sdf</patientCode>
<internalPatientId>sdfsdfsdfsdfsdf</internalPatientId>
</case>
</caseList>
<caseList>
<case>
<patientCode>654654</patientCode>
<internalPatientId>123654654</internalPatientId>
</case>
</caseList>
我需要的是:
<import xmlns_xsi="http://www.w3.org/2001/XMLSchema-instance" xsi_noNamespaceSchemaLocation="TR-DGU%20Import-Schema%20V2015%20-%20Stand%20M%C3%A4rz%202016.xsd">
<creationDate>2016-05-19</creationDate>
<hospitalCode>A-0000-A</hospitalCode>
<importCasesWithErrors>1</importCasesWithErrors>
<caseList>
<case>
<patientCode>sdf</patientCode>
<internalPatientId>sdfsdfsdfsdfsdf</internalPatientId>
</case>
<case>
<patientCode>654654</patientCode>
<internalPatientId>123654654</internalPatientId>
</case>
</caseList>
现在,我有“import”类,包含“caseList”。 “caseList”包含“caseItems”类,然后包含“cases”类。
继承守则:
import.cs
[XmlRoot("import")]
public class Import
{
[XmlAttribute("xmlns_xsi")]
public string XMLNS { get; set; }
[XmlAttribute("xsi_noNamespaceSchemaLocation")]
public string XMLNSLocation { get; set; }
[XmlElement("creationDate")]
public string CreationDate { get; set; }
[XmlElement("hospitalCode")]
public string HospitalCode { get; set; }
[XmlElement("importCasesWithErrors")]
public int ImportCasesWithErrors { get; set; }
[XmlElement("caseList")]
public List<CaseItem> caseList { get; set; }
//[XmlElement("masterData")]
//public MasterData MasterData { get; set; }
public Import(List<CaseItem> caseItemList)
{
caseList = caseItemList;
}
public Import()
{
}
}
CaseList.cs
[Serializable]
[XmlRoot("Import")]
public class CaseList
{
[XmlElement("caseList")]
public List<CaseItem> caseList
{
get;
set;
}
public CaseList(List<CaseItem> caseItemList)
{
caseList = caseItemList;
}
public CaseList()
{
}
}
CaseItem.cs
[Serializable]
[XmlRoot("import")]
public class CaseItem
{
//[XmlElement("masterdata")]
//public MasterData masterdata { get; set; }
[XmlElement("case")]
public Cases cases { get; set; }
public CaseItem(Cases cases)
{
this.cases = cases;
}
public CaseItem()
{
}
}
Cases.cs
public class Cases
{
[XmlElement("patientCode")]
public string PatientCode { get; set; }
[XmlElement("internalPatientId")]
public string InternalPatientId { get; set; }
public Cases()
{
}
}
最后是初始化整个事情的部分:
private static Import CreateExportDGU()
{
List<CaseItem> caseItemList = new List<CaseItem>();
Cases case1 = new Cases();
case1.InternalPatientId = "sdfsdfsdfsdfsdf";
case1.PatientCode = "sdf";
CaseItem CaseItem1 = new CaseItem(case1);
caseItemList.Add(CaseItem1);
Cases case2 = new Cases();
case2.InternalPatientId = "123654654";
case2.PatientCode = "654654";
CaseItem CaseItem2 = new CaseItem(case2);
caseItemList.Add(CaseItem2);
Import import = new Import(caseItemList);
import.XMLNS = "http://www.w3.org/2001/XMLSchema-instance";
import.XMLNSLocation = "TR-DGU%20Import-Schema%20V2015%20-%20Stand%20M%C3%A4rz%202016.xsd";
import.CreationDate = "2016-05-19";
import.HospitalCode = "A-0000-A";
import.ImportCasesWithErrors = 1;
return import;
}
导入将被序列化。
非常感谢任何帮助!
祝你好运 桑德罗
答案 0 :(得分:0)
使用XmlArray属性表示数组,将列表对象类型表示为XmlArrayItem属性。您还需要完全分解出CaseItem。
import.cs
[XmlRoot("import")]
public class Import
{
[XmlAttribute("xmlns_xsi")]
public string XMLNS { get; set; }
[XmlAttribute("xsi_noNamespaceSchemaLocation")]
public string XMLNSLocation { get; set; }
[XmlElement("creationDate")]
public string CreationDate { get; set; }
[XmlElement("hospitalCode")]
public string HospitalCode { get; set; }
[XmlElement("importCasesWithErrors")]
public int ImportCasesWithErrors { get; set; }
[XmlArray("caseList")]
[XmlArrayItem("case")]
public List<Cases> caseList { get; set; }
//[XmlElement("masterData")]
//public MasterData MasterData { get; set; }
public Import(List<Cases> caseItemList)
{
caseList = caseItemList;
}
public Import()
{
}
}
CaseList.cs
[Serializable]
[XmlRoot("Import")]
public class CaseList
{
[XmlElement("caseList")]
public List<Cases> caseList
{
get;
set;
}
public CaseList(List<Cases> caseItemList)
{
caseList = caseItemList;
}
public CaseList()
{
}
}
Cases.cs
public class Cases
{
[XmlElement("patientCode")]
public string PatientCode { get; set; }
[XmlElement("internalPatientId")]
public string InternalPatientId { get; set; }
public Cases()
{
}
}
最后是初始化整个事情的部分:
{
List<Cases> caseItemList = new List<Cases>();
Cases case1 = new Cases();
case1.InternalPatientId = "sdfsdfsdfsdfsdf";
case1.PatientCode = "sdf";
caseItemList.Add(case1);
Cases case2 = new Cases();
case2.InternalPatientId = "123654654";
case2.PatientCode = "654654";
caseItemList.Add(case2);
Import import = new Import(caseItemList);
import.XMLNS = "http://www.w3.org/2001/XMLSchema-instance";
import.XMLNSLocation = "TR-DGU%20Import-Schema%20V2015%20-%20Stand%20M%C3%A4rz%202016.xsd";
import.CreationDate = "2016-05-19";
import.HospitalCode = "A-0000-A";
import.ImportCasesWithErrors = 1;
var s = new System.Xml.Serialization.XmlSerializer(import.GetType());
s.Serialize(Console.Out, import);
}