EDN Admin
Well-known member
Hi, I am trying to figure out if it is possible to use XML Serializer to serialize an array of arbitrary objects. The objects I am serializing are all XML serializer on their own, but when put into an object[] they are not. For example:
public class Foo<br/>
{<br/>
public string Value { get; set; }<br/>
}
public class Bar<br/>
{<br/>
public int Value { get; set; }<br/>
}
public class MyList<br/>
{<br/>
public List<object> List { get; set; }<br/>
}
Assuming I have a method which serializes an individual object Serialize:
var foo = new Foo() { Value = "test" };<br/>
var bar = new Bar() { Value = 123456 };<br/>
var list = new List<object>();<br/>
list.Add(foo);<br/>
list.Add(bar);<br/>
var myList = new MyList() { List = list };<br/>
Serialize(foo); // succeeds<br/>
Serialize(bar); // succeeds<br/>
Serialize(myList); // fails
The XML I would hope for is something list:
<MyList><br/>
<ArrayOfArbitraryObjects><br/>
<Foo><br/>
<Value>test</Value><br/>
</Foo><br/>
<Bar><br/>
<Value>test</Value><br/>
</Bar><br/>
</ArrayOfArbitraryObjects><br/>
</MyList>
If more type information is necessary for each object, I am happy to let the XML serializer inject it. But I cant seem to be able to do it...
Any help is much appreciated!
View the full article
public class Foo<br/>
{<br/>
public string Value { get; set; }<br/>
}
public class Bar<br/>
{<br/>
public int Value { get; set; }<br/>
}
public class MyList<br/>
{<br/>
public List<object> List { get; set; }<br/>
}
Assuming I have a method which serializes an individual object Serialize:
var foo = new Foo() { Value = "test" };<br/>
var bar = new Bar() { Value = 123456 };<br/>
var list = new List<object>();<br/>
list.Add(foo);<br/>
list.Add(bar);<br/>
var myList = new MyList() { List = list };<br/>
Serialize(foo); // succeeds<br/>
Serialize(bar); // succeeds<br/>
Serialize(myList); // fails
The XML I would hope for is something list:
<MyList><br/>
<ArrayOfArbitraryObjects><br/>
<Foo><br/>
<Value>test</Value><br/>
</Foo><br/>
<Bar><br/>
<Value>test</Value><br/>
</Bar><br/>
</ArrayOfArbitraryObjects><br/>
</MyList>
If more type information is necessary for each object, I am happy to let the XML serializer inject it. But I cant seem to be able to do it...
Any help is much appreciated!
View the full article