我的序列化存在问题。我需要实现的是以下xml输出:
<?xml version="1.0" encoding="utf-8"?>
<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>A</patientCode>
<internalPatientId>123456</internalPatientId>
</case>
<case>
<patientCode>B</patientCode>
<internalPatientId>asdasdasdasd</internalPatientId>
</case>
</caseList>
</Import>
但我得到的是以下输出以及我需要摆脱的额外xml级别(注意条目“CaseNotNeeded”):
<?xml version="1.0" encoding="utf-8"?>
<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>
**<CaseNotNeeded>**
<case>
<patientCode>A</patientCode>
<internalPatientId>123456</internalPatientId>
</case>
<case>
<patientCode>B</patientCode>
<internalPatientId>asdasdasdasd</internalPatientId>
</case>
**</CaseNotNeeded>**
</caseList>
</Import>
我需要在List(CaseList)中有一个案例列表(CaseItems),但我总是得到这个额外的条目。有人想知道怎么摆脱它吗?
非常感谢任何提示!
我的代码如下所示:
CaseList.cs
namespace Serialization.Model
{
[Serializable]
[XmlRoot("Import")]
public class CaseList
{
[XmlArrayItem("case", typeof(CaseItem))]
public List<CaseItem> CaseNotNeeded
{
get;
set;
}
public CaseList()
{
CaseNotNeeded = new List<CaseItem>();
}
}
}
CaseItem.cs
namespace Serialization.Model
{
[Serializable]
//[XmlRoot("case")]
public class CaseItem
{
[XmlElement("patientCode")]
public string PatientCode { get; set; }
[XmlElement("internalPatientId")]
public string InternalPatientId { get; set; }
public CaseItem()
{
}
}
}
Import.cs
namespace Serialization.Model
{
[Serializable]
[XmlRootAttribute(ElementName = "Import", IsNullable = false)]
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 CaseList CaseList { get; set; }
public Import()
{
}
}
}
最后在主要方法中:
.............
.............
.............
import.CaseList = new CaseList();
CaseItem case_1 = new CaseItem();
case_1.InternalPatientId = "123456";
case_1.PatientCode = "A";
import.CaseList.CaseNotNeeded.Add(case_1);
CaseItem case_2 = new CaseItem();
case_2.InternalPatientId = "asdasdasdasd";
case_2.PatientCode = "B";
import.CaseList.CaseNotNeeded.Add(case_2);