I have an issue with my serialization. what I need to achieve is the following xml output:
<?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> <intealPatientId>123456</intealPatientId> </case> <case> <patientCode>B</patientCode> <intealPatientId>asdasdasdasd</intealPatientId> </case> </caseList>
</Import>
But what I get is the following output with an additional xml level which I need to get rid of (Note the Entry "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> <intealPatientId>123456</intealPatientId> </case> <case> <patientCode>B</patientCode> <intealPatientId>asdasdasdasd</intealPatientId> </case> **</CaseNotNeeded>** </caseList>
</Import>
I need to have a list of cases (CaseItems) in the List (CaseList) but I get always this additional entry. Anybody an idea how to get rid of that?
Any hint is very much appreciated!
My code looks like this:
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("intealPatientId")] public string IntealPatientId { 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() { } }
}
and finally in the main method:
.............
.............
.............
import.CaseList = new CaseList();
CaseItem case_1 = new CaseItem();
case_1.IntealPatientId = "123456";
case_1.PatientCode = "A";
import.CaseList.CaseNotNeeded.Add(case_1);
CaseItem case_2 = new CaseItem();
case_2.IntealPatientId = "asdasdasdasd";
case_2.PatientCode = "B";
import.CaseList.CaseNotNeeded.Add(case_2);