4

there. I have the foloowing class definition:

[Serializable]
public enum FilterType
{
    [XmlEnum("1")]
    Text = 1,
     [XmlEnum("2")]
    Date = 2,
     [XmlEnum("3")]
    Combo = 3,
     [XmlEnum("4")]
    Multichoice = 4
}

public class FilterValues : List<string>
{
    public FilterType Type { get; set; } 
}

[Serializable]
public struct SerializableKeyValuePair<K, V>
{
    public SerializableKeyValuePair(KeyValuePair<K, V> p)
    {
        this.key = p.Key;
        this.value = p.Value;
    }

    private K key;
    public K Key
    {
        get { return key; }
        set { key = value; }
    }

    private V value;
    public V Value
    {
        get { return this.value; }
        set { this.value = value; }
    }
}

and I can't make it to serialize the Type property in xml when I try to serialize array of SerializableKeyValuePair with key of type string and value of type FilterValues ( SerializableKeyValuePair<string, FilterValues>[] ). I got this result:

<?xml version="1.0" encoding="utf-16"?>  
<ArrayOfSerializableKeyValuePairOfStringFilterValues xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">    
<SerializableKeyValuePairOfStringFilterValues>      
<Key>Date</Key>     
 <Value>        <string>2012-5-16</string>      </Value>    
 </SerializableKeyValuePairOfStringFilterValues>    
 <SerializableKeyValuePairOfStringFilterValues>      
 <Key>bgName</Key>      <Value>        <string>4</string>      </Value>    
 </SerializableKeyValuePairOfStringFilterValues>  
 </ArrayOfSerializableKeyValuePairOfStringFilterValues>

Please help, I tried anything possible.

Guffa
  • 640,220
  • 96
  • 678
  • 956
user1396456
  • 43
  • 1
  • 3
  • As a side note: `Serializable` attribute is irrelevant for XML serialization - only binary serialization. – Konrad Morawski May 16 '12 at 09:17
  • I found out that the problem is not in the enum. Because If I make a class that has a member of type List and a member of FilterType, there is no problem with serialization. It seems like inheritance of List brings the trouble – user1396456 May 16 '12 at 11:10

1 Answers1

1

It is related to this question: When a class is inherited from List<>, XmlSerializer doesn't serialize other attributes

So, as pointed out in answer to that question, you have to change your FilterValues implementation in one of at least three possible ways:

-implement IXmlSerializable
-remove inheritance from List
-use another serializer
Community
  • 1
  • 1
6opuc
  • 1,116
  • 3
  • 13
  • 21