Note that there are some explanatory texts on larger screens.

plurals
  1. POSerializing List<> leads to double tags
    primarykey
    data
    text
    <p>Attached are the following files:</p> <pre><code>facility.xsd facilities_v1.designer.cs (generated from facility.xsd) facilites.xml (sample output) facilites_wish.xml (expected output) test.cs (contains a method which is used to create some sample objects and the serialisation) </code></pre> <p><a href="https://dl.dropbox.com/u/481455/fac.zip" rel="nofollow">https://dl.dropbox.com/u/481455/fac.zip</a></p> <p>Serializing works find, but the results differ from what I've expected:</p> <p>The lists are serialized "twice" (please see facilities.xml). The expected results are realized in facilities_wish.xml</p> <p>The facilities.xsd:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="facilities_v1.dtd" xmlns:wmh="http://www.wmhelp.com/2003/eGenerator" elementFormDefault="qualified" targetNamespace="facilities_v1.dtd"&gt; &lt;xs:element name="facilities"&gt; &lt;xs:annotation&gt; &lt;xs:documentation&gt; $Id$ &lt;/xs:documentation&gt; &lt;xs:documentation&gt; ============================================================ &lt;/xs:documentation&gt; &lt;xs:documentation&gt; For further information or questions, please contact Michael Balmer, balmermi at inf dot ethz dot ch &lt;/xs:documentation&gt; &lt;xs:documentation&gt; ============================================================ &lt;/xs:documentation&gt; &lt;/xs:annotation&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="facility" minOccurs="0" maxOccurs="unbounded"/&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name="name" type="xs:string"/&gt; &lt;xs:attribute name="aggregation_layer" type="xs:string"/&gt; &lt;xs:attribute name="lang" type="xs:string" default="de-CH"/&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="facility"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="activity" minOccurs="0" maxOccurs="unbounded"/&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name="id" type="xs:string" use="required"/&gt; &lt;xs:attribute name="x" type="xs:string" use="required"/&gt; &lt;xs:attribute name="y" type="xs:string" use="required"/&gt; &lt;xs:attribute name="desc" type="xs:string"/&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="activity"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="capacity" minOccurs="0"/&gt; &lt;xs:element ref="opentime" minOccurs="0" maxOccurs="unbounded"/&gt; &lt;/xs:sequence&gt; &lt;xs:attribute name="type" type="xs:string" use="required"/&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="capacity"&gt; &lt;xs:complexType&gt; &lt;xs:attribute name="value" type="xs:string" use="required"/&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="opentime"&gt; &lt;xs:complexType&gt; &lt;xs:attribute name="day" default="wk"&gt; &lt;xs:simpleType&gt; &lt;xs:restriction base="xs:NMTOKEN"&gt; &lt;xs:enumeration value="mon"/&gt; &lt;xs:enumeration value="tue"/&gt; &lt;xs:enumeration value="wed"/&gt; &lt;xs:enumeration value="thu"/&gt; &lt;xs:enumeration value="fri"/&gt; &lt;xs:enumeration value="sat"/&gt; &lt;xs:enumeration value="sun"/&gt; &lt;xs:enumeration value="wkday"/&gt; &lt;xs:enumeration value="wkend"/&gt; &lt;xs:enumeration value="wk"/&gt; &lt;/xs:restriction&gt; &lt;/xs:simpleType&gt; &lt;/xs:attribute&gt; &lt;xs:attribute name="start_time" type="xs:string" use="required"/&gt; &lt;xs:attribute name="end_time" type="xs:string" use="required"/&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; </code></pre> <p>The serializer classes:</p> <pre><code>{ 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 facilities { private List&lt;facility&gt; facilityField; private string nameField; private string aggregation_layerField; private string langField; public facilities() { this.facilityField = new List&lt;facility&gt;(); this.langField = "de-CH"; } public List&lt;facility&gt; facility { get { return this.facilityField; } set { this.facilityField = value; } } public string name { get { return this.nameField; } set { this.nameField = value; } } public string aggregation_layer { get { return this.aggregation_layerField; } set { this.aggregation_layerField = value; } } [System.ComponentModel.DefaultValueAttribute("de-CH")] public string lang { get { return this.langField; } set { this.langField = value; } } } public partial class facility { private List&lt;activity&gt; activityField; private string idField; private string xField; private string yField; private string descField; public facility() { this.activityField = new List&lt;activity&gt;(); } public List&lt;activity&gt; activity { get { return this.activityField; } set { this.activityField = value; } } public string id { get { return this.idField; } set { this.idField = value; } } public string x { get { return this.xField; } set { this.xField = value; } } public string y { get { return this.yField; } set { this.yField = value; } } public string desc { get { return this.descField; } set { this.descField = value; } } } public partial class activity { private capacity capacityField; private List&lt;opentime&gt; opentimeField; private string typeField; public activity() { this.opentimeField = new List&lt;opentime&gt;(); this.capacityField = new capacity(); } public capacity capacity { get { return this.capacityField; } set { this.capacityField = value; } } public List&lt;opentime&gt; opentime { get { return this.opentimeField; } set { this.opentimeField = value; } } public string type { get { return this.typeField; } set { this.typeField = value; } } } public partial class capacity { private string valueField; public string value { get { return this.valueField; } set { this.valueField = value; } } } public partial class opentime { private opentimeDay dayField; private string start_timeField; private string end_timeField; public opentime() { this.dayField = opentimeDay.wk; } [System.ComponentModel.DefaultValueAttribute(opentimeDay.wk)] public opentimeDay day { get { return this.dayField; } set { this.dayField = value; } } public string start_time { get { return this.start_timeField; } set { this.start_timeField = value; } } public string end_time { get { return this.end_timeField; } set { this.end_timeField = value; } } } public enum opentimeDay { /// &lt;remarks/&gt; mon, /// &lt;remarks/&gt; tue, /// &lt;remarks/&gt; wed, /// &lt;remarks/&gt; thu, /// &lt;remarks/&gt; fri, /// &lt;remarks/&gt; sat, /// &lt;remarks/&gt; sun, /// &lt;remarks/&gt; wkday, /// &lt;remarks/&gt; wkend, /// &lt;remarks/&gt; wk, } } </code></pre> <p>Sample output: </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;facilities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;facility&gt; &lt;facility&gt; &lt;activity&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;10000&lt;/value&gt; &lt;/capacity&gt; &lt;opentime /&gt; &lt;type&gt;accommodation&lt;/type&gt; &lt;/activity&gt; &lt;/activity&gt; &lt;id&gt;AC5&lt;/id&gt; &lt;x&gt;1,5&lt;/x&gt; &lt;y&gt;1,5&lt;/y&gt; &lt;desc&gt;IOC Bus Hub&lt;/desc&gt; &lt;/facility&gt; &lt;facility&gt; &lt;activity&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;198&lt;/value&gt; &lt;/capacity&gt; &lt;opentime&gt; &lt;opentime&gt; &lt;day&gt;sat&lt;/day&gt; &lt;start_time&gt;16:00&lt;/start_time&gt; &lt;end_time&gt;21:30&lt;/end_time&gt; &lt;/opentime&gt; &lt;/opentime&gt; &lt;type&gt;605_ATH&lt;/type&gt; &lt;/activity&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;380&lt;/value&gt; &lt;/capacity&gt; &lt;opentime&gt; &lt;opentime&gt; &lt;day&gt;sat&lt;/day&gt; &lt;start_time&gt;12:30&lt;/start_time&gt; &lt;end_time&gt;23:00&lt;/end_time&gt; &lt;/opentime&gt; &lt;/opentime&gt; &lt;type&gt;605_MPB&lt;/type&gt; &lt;/activity&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;380&lt;/value&gt; &lt;/capacity&gt; &lt;opentime&gt; &lt;opentime&gt; &lt;day&gt;sat&lt;/day&gt; &lt;start_time&gt;15:30&lt;/start_time&gt; &lt;end_time&gt;00:00&lt;/end_time&gt; &lt;/opentime&gt; &lt;/opentime&gt; &lt;type&gt;605_MPP&lt;/type&gt; &lt;/activity&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;95&lt;/value&gt; &lt;/capacity&gt; &lt;opentime&gt; &lt;opentime&gt; &lt;day&gt;sat&lt;/day&gt; &lt;start_time&gt;12:45&lt;/start_time&gt; &lt;end_time&gt;22:00&lt;/end_time&gt; &lt;/opentime&gt; &lt;/opentime&gt; &lt;type&gt;605_OBO&lt;/type&gt; &lt;/activity&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;1061&lt;/value&gt; &lt;/capacity&gt; &lt;opentime&gt; &lt;opentime&gt; &lt;day&gt;sat&lt;/day&gt; &lt;start_time&gt;13:00&lt;/start_time&gt; &lt;end_time&gt;00:00&lt;/end_time&gt; &lt;/opentime&gt; &lt;/opentime&gt; &lt;type&gt;605_WF&lt;/type&gt; &lt;/activity&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;380&lt;/value&gt; &lt;/capacity&gt; &lt;opentime&gt; &lt;opentime&gt; &lt;day&gt;sat&lt;/day&gt; &lt;start_time&gt;16:15&lt;/start_time&gt; &lt;end_time&gt;21:00&lt;/end_time&gt; &lt;/opentime&gt; &lt;/opentime&gt; &lt;type&gt;605_OF&lt;/type&gt; &lt;/activity&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;5000&lt;/value&gt; &lt;/capacity&gt; &lt;opentime&gt; &lt;opentime&gt; &lt;day&gt;sat&lt;/day&gt; &lt;start_time&gt;15:15&lt;/start_time&gt; &lt;end_time&gt;21:30&lt;/end_time&gt; &lt;/opentime&gt; &lt;/opentime&gt; &lt;type&gt;605_SPE&lt;/type&gt; &lt;/activity&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;1000&lt;/value&gt; &lt;/capacity&gt; &lt;opentime&gt; &lt;opentime&gt; &lt;day&gt;sat&lt;/day&gt; &lt;start_time&gt;15:30&lt;/start_time&gt; &lt;end_time&gt;21:00&lt;/end_time&gt; &lt;/opentime&gt; &lt;/opentime&gt; &lt;type&gt;605_SPO&lt;/type&gt; &lt;/activity&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;11&lt;/value&gt; &lt;/capacity&gt; &lt;opentime&gt; &lt;opentime&gt; &lt;day&gt;sat&lt;/day&gt; &lt;start_time&gt;14:30&lt;/start_time&gt; &lt;end_time&gt;22:00&lt;/end_time&gt; &lt;/opentime&gt; &lt;/opentime&gt; &lt;type&gt;605_TO&lt;/type&gt; </code></pre> <p>Wished Output:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;facilities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;facility&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;10000&lt;/value&gt; &lt;/capacity&gt; &lt;opentime /&gt; &lt;type&gt;accommodation&lt;/type&gt; &lt;/activity&gt; &lt;id&gt;AC5&lt;/id&gt; &lt;x&gt;1,5&lt;/x&gt; &lt;y&gt;1,5&lt;/y&gt; &lt;desc&gt;IOC Bus Hub&lt;/desc&gt; &lt;/facility&gt; &lt;facility&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;198&lt;/value&gt; &lt;/capacity&gt; &lt;opentime&gt; &lt;opentime&gt; &lt;day&gt;sat&lt;/day&gt; &lt;start_time&gt;16:00&lt;/start_time&gt; &lt;end_time&gt;21:30&lt;/end_time&gt; &lt;/opentime&gt; &lt;/opentime&gt; &lt;type&gt;605_ATH&lt;/type&gt; &lt;/activity&gt; &lt;activity&gt; &lt;capacity&gt; &lt;value&gt;380&lt;/value&gt; &lt;/capacity&gt; &lt;opentime&gt; &lt;opentime&gt; &lt;day&gt;sat&lt;/day&gt; &lt;start_time&gt;12:30&lt;/start_time&gt; &lt;end_time&gt;23:00&lt;/end_time&gt; &lt;/opentime&gt; &lt;/opentime&gt; &lt;type&gt;605_MPB&lt;/type&gt; &lt;/activity&gt; &lt;/facility&gt; &lt;/facilities&gt; Object Creation: [TestMethod] public void TestAlex() { var hashFacilities = new Hashtable(); var facXlsx = new Workbook(ConfigSettings.ReadSetting("facilitiesXLSX")); Worksheet wFacilities = facXlsx.Worksheets["Tabelle2"]; for (int i = 3; i &lt; wFacilities.Cells.Rows.Count; i++) { Row row = wFacilities.Cells.Rows[i]; string valName = row.GetCellByIndex(0).StringValue; string valAggregationlayer = row.GetCellByIndex(1).StringValue; string valLang = row.GetCellByIndex(2).StringValue; string valId = row.GetCellByIndex(3).StringValue; string valX = row.GetCellByIndex(4).StringValue; string valY = row.GetCellByIndex(5).StringValue; string valDesc = row.GetCellByIndex(6).StringValue; string valType = row.GetCellByIndex(7).StringValue; string valValue = row.GetCellByIndex(8).StringValue; string valDay = row.GetCellByIndex(9).StringValue; string valStartTime = row.GetCellByIndex(10).StringValue; string valEndTime = row.GetCellByIndex(11).StringValue; if (hashFacilities.ContainsKey(valId)) { var fac = (facility) hashFacilities[valId]; AttachActivity(valType, valValue, valDay, valStartTime, valEndTime, fac); } else { var fac = new facility {id = valId, desc = valDesc, x = valX, y = valY}; AttachActivity(valType, valValue, valDay, valStartTime, valEndTime, fac); hashFacilities.Add(valId, fac); } } var facilities = new facilities(); facilities.facility.AddRange(hashFacilities.Values.Cast&lt;facility&gt;().ToList()); var writer = new XmlSerializer(typeof (facilities)); var file = new StreamWriter(@"c:\temp\facilities.xml"); writer.Serialize(file, facilities); file.Close(); } private static void AttachActivity(string valType, string valValue, string valDay, string valStartTime, string valEndTime, facility fac) { var activity = new activity {type = valType, capacity = new capacity {value = valValue}}; if (!string.IsNullOrEmpty(valDay) &amp;&amp; !string.IsNullOrEmpty(valStartTime) &amp;&amp; !string.IsNullOrEmpty(valEndTime)) { var time = new opentime(); opentimeDay day; Enum.TryParse(valDay, true, out day); time.day = day; time.start_time = valStartTime; time.end_time = valEndTime; activity.opentime = new List&lt;opentime&gt; {time}; } if (fac.activity != null) { fac.activity.Add(activity); } else { fac.activity = new List&lt;activity&gt; {activity}; } } } &lt;/xs:schema&gt; </code></pre> <p>What is here going wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload