XmlSerializing an array of CollectionBase

M

Morten Dyndgaard

Hi,

I want to do the following:

I have to XmlSerialize a HashTable, but since it inherits from
IDictionary it cannot be XmlSerialized, and I prefer not to make my
own implementation of IXmlSerializer.

So, I make two arrays, one containing the keys and one containing the
values, and I would like to XmlSerialize these arrays. However, the
values are instances of a class that inherits from CollectionBase.

If I didn't have arrays of objects that inherits from CollectionBase
everything would be fine. The same goes if my objects did not inherit
from CollectionBase. But the combination results in the error:

File or assembly name <random name>.dll, or one of its depencencies,
was not found.

How can I make it work?



My code looks roughly like this:

public class XmlTest
{
public class MyColl : CollectionBase
{
public void Add (string uo) {}

public string this [int index] {get { return "hello";}}
}

[Serializable]
public class SerializeThis
{
[XmlArray]
[XmlArrayItem(Type = typeof(MyColl))]
public MyColl[] myInfo;

public SerializeThis ()
{
}

public SerializeThis (MyColl[] myInfo)
{
this.myInfo = myInfo;
}
}

public void Run (MyColl[] myInfo)
{
StringWriter stringWriter = new StringWriter ();

SerializeThis serializeThis = new SerializeThis();
serializeThis.myInfo = myInfo;

XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);
xmlWriter.Formatting = Formatting.Indented;
new XmlSerializer(typeof (SerializeThis)).Serialize (xmlWriter,
serializeThis);
xmlWriter.Flush ();
}
}

Morten
 
J

Jens Hofmann

Create something like this :

public class Entry
{
public string Key;
public string Value;
}

public class EntryList : CollectionBase
{
public Entry Add(Entry newEntry)
{
return this[List.Add(newEntry)];
}

public Entry this[int index]
{
get{return (Entry) List[index];}
}
}

This shouldn´'t be a problem for a webservice to serialize.

Cu Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top