XmlSerializer serializes helper objects
How do you serialize the following
[XmlRoot("response")]
public class MyCollection<T>
{
[XmlElement("person", Type = typeof(Person))]
public List<T> entry;
public int startIndex;
}
where T can be a class of type
public class Person
{
public string name;
}
in
<response>
<startIndex>1</startIndex>
<entry>
<person>
<name>meeee</name>
</person>
</entry>
<entry>
<person>
<name>youuu</name>
</person>
</entry>
</response>
I played with [XmlArray], [XmlArrayItem] and [XmlElement], and I cannot get the right combination. Arrrgghhh.
Update:
[XmlArray("entry")]
[XmlArrayItem("person", Type = typeof(Person))]
public List<T> entry;
gives me
<entry><person></person><person></person></entry>
[XmlElement("person", Type = typeof(Person))]
public List<T> entry;
gives me
<person></person><person></person>
I don’t see any obvious way to make it output these results without drastically changing classes ... it may not be what you want, but by mirroring the desired result (not uncommon in DTO), it gets the right result ...
Otherwise, you can watch IXmlSerializable, which is a huge pain:
using System;
using System.Collections.Generic;
using System.Xml.Serialization;
[XmlRoot("response")]
public class MyResponse {
public MyResponse() {
Entries = new List<Entry>();
}
[XmlElement("startIndex", Order = 1)]
public int StartIndex { get; set; }
[XmlElement("entry", Order = 2)]
public List<Entry> Entries { get; set; }
}
public class Entry {
public Entry() { }
public Entry(Person person) { Person = person; }
[XmlElement("person")]
public Person Person { get; set; }
public static implicit operator Entry(Person person) {
return person == null ? null : new Entry(person);
}
public static implicit operator Person(Entry entry) {
return entry == null ? null : entry.Person;
}
}
public class Person {
[XmlElement("name")]
public string Name { get; set; }
}
static class Program {
static void Main() {
MyResponse resp = new MyResponse();
resp.StartIndex = 1;
resp.Entries.Add(new Person { Name = "meeee" });
resp.Entries.Add(new Person { Name = "youuu" });
XmlSerializer ser = new XmlSerializer(resp.GetType());
ser.Serialize(Console.Out, resp);
}
}
IXmlSerializable. XmlDictionaryWriter , DataContractSerializer. MyCollection
public interface IMyCollection
{
int getStartIndex();
IList getEntry();
}
MyCollection XMLxxxx. . ?
public string ConvertToXML(object obj)
{
MemoryStream ms = new MemoryStream();
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(ms, Encoding.UTF8, true))
{
writer.WriteStartDocument();
if (obj is IMyCollection)
{
IMyCollection collection = (IMyCollection)obj;
writer.WriteStartElement("response");
writer.WriteElementString("startIndex","0");
var responses = collection.getEntry();
foreach (var item in responses)
{
writer.WriteStartElement("entry");
DataContractSerializer ser = new DataContractSerializer(item.GetType());
ser.WriteObject(writer, item);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
writer.Flush();
return Encoding.UTF8.GetString(ms.ToArray());
}
This seems like a similar problem that I am facing. I finally hacked it and posted everything to the question here .
XML Serialization and Inherited Types
Is it used?