0

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>
                <internalPatientId>123456</internalPatientId>
            </case>
            <case>
                <patientCode>B</patientCode>
                <internalPatientId>asdasdasdasd</internalPatientId>
            </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>
                <internalPatientId>123456</internalPatientId>
            </case>
            <case>
                <patientCode>B</patientCode>
                <internalPatientId>asdasdasdasd</internalPatientId>
            </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("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()
        { 
        }
    }
}

and finally in the main method:

.............
.............
.............
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);
Martin Felber
  • 121
  • 17
  • Have you inspected the `import` object before serializing it? Does it contain an additional `CaseItem`? – Ulric May 20 '16 at 13:27
  • 1
    Why do you need the CaseList class? Can't you just make `CaseList` in the `Import` class a type of `List`? – entropic May 20 '16 at 13:27
  • Thx for the quick responses. I'll need to do some more inspection of code, maybe I was zto quick with my answer, sry :) – Martin Felber May 20 '16 at 13:39
  • This is a duplicate of [XML Serialization - Disable rendering root element of array](https://stackoverflow.com/questions/2006482/xml-serialization-disable-rendering-root-element-of-array). See https://dotnetfiddle.net/KY7GBe for a demo of using `[XmlElement("case")]` on your `CaseList` class. – dbc May 20 '16 at 19:32

0 Answers0