Serge Misnik
Serge Misnik

Reputation: 307

C# class serialization mapping

I'm looking for hints, ideas on how to implement and/or change class serialization behavior of following classes in general.

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-in and serialize Choobakka's stuff

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 looks like:

<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 to get rig of <Items> tag (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 VariableList<T> class, except applying some XML serialization attributes.

The reason for this is not just simplifying the resulting XML, but also desearializing XML generated by SQL Server queries. I have thoughts of attributes, xsd/xslt transformations...

Upvotes: 5

Views: 1874

Answers (3)

Sinatr
Sinatr

Reputation: 21999

If you can't change VariableList<>, then changing Choobaka will do

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
            // ...
        }
    }
}

produces (proof)

<?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>

Upvotes: 1

Eldar
Eldar

Reputation: 104

Attribute help.

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

output:

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

Further reading

Upvotes: 0

jdweng
jdweng

Reputation: 34421

Just add 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>();
        }
    }

Upvotes: 0

Related Questions