1

I'm attempting to serialize (and subsequently deserialize) a rather simple class to an XML string, but am getting an exception: "The type System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] may not be used in this context."

The method I'm using to serialize is:

public string ToXml(TaskListFilterConfig config)
{
    Type[] extraTypes = { typeof(FilterConfig), typeof(SortConfig) };

    XmlSerializer serializer = new XmlSerializer(config.GetType(), extraTypes);

    using (StringWriter writer = new StringWriter())
    {
        serializer.Serialize(writer, config);

        return writer.ToString();
    }
}

The classes I'm attempting to serialize are:

[XmlRoot(ElementName = "TaskListFilterConfig", IsNullable = false)]
[XmlInclude(typeof(FilterConfig))]
[XmlInclude(typeof(SortConfig))]
public class TaskListFilterConfig
{
    [XmlArray("FilterConfigList")]
    [XmlArrayItem("FilterConfig")]
    public List<FilterConfig> FilterConfigList { get; set; }

    [XmlArray("SortConfigList")]
    [XmlArrayItem("SortConfig")]
    public List<SortConfig> SortConfigList { get; set; }

    public TaskListFilterConfig() 
    {
        FilterConfigList = new List<FilterConfig>();
        SortConfigList = new List<SortConfig>();
    }
}


[XmlType("FilterConfig")]
public class FilterConfig
{

    public OperandType Operand { get; set; }

    public int SelectedOperatorIndex { get; set; }

    public int SelectedColumnIndex { get; set; }

    public object RightOperand { get; set; }

    public FilterConfig() { }
}

[XmlType("SortConfig")]
public class SortConfig
{
    public Infragistics.Windows.Controls.SortStatus SortDirection { get; set; }

    public int ColumnSelectedIndex { get; set; }

    public SortConfig() { }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Dustin Kofoed
  • 539
  • 1
  • 8
  • 17
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Oct 10 '13 at 18:05
  • 1
    Exactly what line do you get that exception on, and please post the _entire_ exception. Use `ex.ToString()`. The inner exceptions from XML Serializer often contain a lot of detail. – John Saunders Oct 10 '13 at 18:06
  • 1
    NB: all of the XmlXxx attributes in your code sample are unnecessary, and neither is the specification of extra types to XmlSerializer ctor. – Anton Tykhyy Oct 10 '13 at 18:08
  • I was getting a similar exception without all the XMLxxx attributes, and have added them since to try and resolve it (as per this [link](http://stackoverflow.com/questions/1212742/xml-serialize-generic-list-of-serializable-objects)) – Dustin Kofoed Oct 10 '13 at 18:34
  • As for further exception details - there really isn't a whole lot more, that came from the inner exception actually. The parent exception was not helpful ("There was an error generating the XML document"). The line it fails on is the actual call to the Serialize method. – Dustin Kofoed Oct 10 '13 at 18:38

1 Answers1

0

Your Class Model and Searalizable data are wrong.

Notice that TaskListFilterConfig is a different type than extraTypes. extraTypes has some other model data.

If you want to serialize TaskListFilterConfig class and Data: Use below Code

public string ToXml(TaskListFilterConfig config)
{
    XmlSerializer serializer = new XmlSerializer(typeOf(TaskListFilterConfig));
    using (StringWriter writer = new StringWriter())
    {
        serializer.Serialize(writer, config);
        return writer.ToString();
    }
}

Or if you want extraTypes pass the relevent model data as well.

I know it's a very late answer. Just want to make sure the question has answered. Maybe it help will someone in the future. :)

Patrick Motard
  • 2,650
  • 2
  • 14
  • 23
RajeshKdev
  • 6,365
  • 6
  • 58
  • 80