C # class serialization mapping

I am looking for tips, ideas on how to implement and / or change the serialization behavior of the class of the following classes as a whole.

Simplified (incomplete) sample classes:

[Serializable]
public class Choobakka
{
    public string Name { get; set; }
    public VariableList<Item> Stuff { get; set; }
}
[Serializable]
public class Item
{
    public string Name { get; set; }
    public string Value { get; set; }
}
[Serializable]
public sealed class VariableList<T> : AVariableList<T>
{
    public new ItemList<T> Items { get { return _items; } set { _items = value; } }
    public new bool IsNull { get { return Items == null; } }
    public new bool IsEmpty { get { return IsNull || Count <= 0; } }
    public new int Count { get { return IsNull ? 0 : this.Items.Count; } }
    public new string CountAsString { get { return Count.ToString(); } }
    public VariableList()
    {
        _items = new ItemList<T>();
    }
}

And this is how I fill and serialize the material of Chobakka

var choobakka = new Choobakka() { Name = "CHOO-BAKKA", Stuff = new VariableList<Item>()  };            
choobakka.Stuff.Items.Add( new Item() { Name = "passport", Value = "lv" } );
choobakka.Stuff.Items.Add( new Item() { Name = "wallet", Value = "50euro" } );
StringBuilder sb = new StringBuilder();
using (TextWriter tw = new StringWriter(sb))
{
    XmlSerializer xs = new XmlSerializer(typeof(Choobakka));
    xs.Serialize(tw, choobakka);
}

Serialized XML is as follows:

<Choobakka>
    <Name>CHOO-BAKKA</Name>
    <Stuff>
        <Items>
            <Item>
                <Name>passport</Name>
                <Value>lv</Value>
            </Item>
            <Item>
                <Name>wallet</Name>
                <Value>50euro</Value>
            </Item>
        </Items>
    </Stuff>
</Choobakka>

Now the question is, how would you suggest getting the tag binding <Items>(if possible) to something like

<Choobakka>
    <Name>CHOO-BAKKA</Name>
    <Stuff>
        <Item>
            <Name>passport</Name>
            <Value>lv</Value>
        </Item>
        <Item>
            <Name>wallet</Name>
            <Value>50euro</Value>
        </Item>
    </Stuff>
</Choobakka>

Having said that, I cannot change the structure of the class VariableList<T>, except for using some XML serialization attributes.

The reason for this is not just simplification of the resulting XML, but also the deserialization of the XML generated by SQL Server queries. I have thoughts about attributes, xsd/ xsltconversions ...

+4
3

VariableList<>, Choobaka

public class Choobakka
{
    public string Name { get; set; }
    [XmlIgnore]
    public VariableList<Item> Stuff { get; set; } // we do not serialize this
    [XmlArray("Stuff")]
    public Item[] _Stuff // but this
    {
        get
        {
            // get Item[] from Stuff property somehow
            // ...

            // as test
            return new Item[] {new Item() { Name = "1", Value = "111"}, new Item() { Name = "2", Value = "222"}};
        }
        set
        {
            // set Stuff property from Item[] somehow
            // ...
        }
    }
}

()

<?xml version="1.0" encoding="utf-16"?>
<Choobakka xmlns:xsd="http://www.w3.org/2001/XMLSchema"             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Name>CHOO-BAKKA</Name>
  <Stuff>
    <Item>
      <Name>1</Name>
      <Value>111</Value>
    </Item>
    <Item>
      <Name>2</Name>
      <Value>222</Value>
    </Item>
  </Stuff>
</Choobakka>
+1

.

[Serializable]
public class Choobakka
{
    public string Name { get; set; }
    [XmlElement("ElementName")]
    public VariableList<Item> Stuff { get; set; }
}

:

<Choobakka>
        <Name>CHOO-BAKKA</Name>            
        <ElementName>
            <Name>passport</Name>
            <Value>lv</Value>
        </ElementName>
        <ElementName>
            <Name>wallet</Name>
            <Value>50euro</Value>
        </ElementName>
    </Choobakka>

0

Just add an XmlElement to the class

public sealed class VariableList<T> : AVariableList<T>
    {
        [XmlElement("Item")]
        public new ItemList<T> Items { get { return _items; } set { _items = value; } }
        public new bool IsNull { get { return Items == null; } }
        public new bool IsEmpty { get { return IsNull || Count <= 0; } }
        public new int Count { get { return IsNull ? 0 : this.Items.Count; } }
        public new string CountAsString { get { return Count.ToString(); } }
        public VariableList()
        {
            _items = new ItemList<T>();
        }
    }
Run codeHide result
0
source

Source: https://habr.com/ru/post/1585090/


All Articles