3

I have a class that contains a number of standard fields and an arraylist.

Is there any way to serialize the class using an XmlSerializer?

Attempts so far result in an error message saying:

Unhandled Exception: System.InvalidOperationException: There was an error
generating the XML document. ---> System.InvalidOperationException: The type
XMLSerialization.DataPoints was not  expected. Use  the XmlInclude or
SoapInclude attribute  to specify types that are not known statically.

Some cut-down representations of the classes are shown below:

public class StationData
{
  private DateTime _CollectionDate;
  private string _StationID;
  private ArrayList _PolledData;

  public StationData()
  {
  }
  public DateTime CollectionDate
  {
    get { return _CollectionDate; }
    set { _CollectionDate = value; }
  }
  public string StationID
  {
    get { return _StationID; }
    set { _StationID = value; }
  }
  [XmlInclude(typeof(DataPoints))]
  public ArrayList PolledData
  {
    get { return _PolledData; }
    set { _PolledData = value; }
  }
}

public class DataPoints
{
  private string _SubStationID;
  private int _PointValue;

  public DataPoints
  {
  }
  public string SubStationID
  {
    get { return _SubStationID; }
    set { _SubStationID = value; }
  }
  public int PointValue
  {
    get { return _PointValue; }
    set { _PointValue = value; }
  }
}
1
  • Can you include your class declaration code and your XmlSerialization Code? Commented Mar 19, 2009 at 22:15

5 Answers 5

5

I have had success with the following:

[XmlArray("HasTypeSpecialisations")]
[XmlArrayItem("TypeObject", typeof(TypeObject), IsNullable = false)]
public List<TypeObject> TypeSpecialisations

This results in:

<HasTypeSpecialisations>
    <TypeObject />
    <TypeObject />
</HasTypeSpecialisations>

In your situation I would try something like:

[XmlArrayItem(typeof(DataPoints))]
public ArrayList PolledData

Based on this link http://msdn.microsoft.com/en-us/library/2baksw0z(VS.85).aspx you should also be able to use this

[XmlElement(Type = typeof(DataPoints))]
public ArrayList PolledData
Sign up to request clarification or add additional context in comments.

Comments

3

The XmlSerializer generates some code at runtime to serialize your class. It is necessary for this class to know all types that can occur.

The ArrayList does not give this information, but you can give it by using a XmlInclude attribute on the property that returns the ArrayList.

[XmlInclude(typeof(DataPoints))]
public ArrayList Points {
   ...
}

You could also use the generic List<> class.

1 Comment

This now presents me with a compile time error: Attribute 'XmlInclude' is not valid on this declaration type. It is valid on 'class, struct, method, interface' declarations only
1

I think you could get around this by using a generic list (List<>) instead of an ArrayList, however, I'm going to assume you can't use generics for one reason or another.

You need to tell the compiler what type is contained in the ArrayList so it can serialize it since all it knows it that it contains objects.

Add this above your property and it should clear it up. [System.Xml.Serialization.XmlInclude(typeof(XMLSerialization.DataPoints))]

Of course, replace XMLSerialization.DataPoints with whatever class is contained in the ArrayList.

1 Comment

I've posted some code extracts as the compiler is now complaining about the XmlInclude statement (not valid on this declaration type). I'll look to see if the ArrayList can be replaced by a List<>.
0

Take a look at this article which describes the basic problem you are having and a solution around it (Like using the XmlInclude attribute). Basically what that says is that the serializer encountered a type it doesn't know how to serialize. If you post some code it would also help greatly.

Comments

0

The method I always used to serialize lists was to convert them to Arrays (which has the necessary type information). I admit this is a bit dirty, but if you can't get a proper list to serialize, this will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.