I am trying to serialize a list that contains non-system types.
Below is my serialization code which is working fine on the top level. and returns a valid XmlDocument, but doesn't seem to contain anything in a inner list.
I've looked around the net - and around SO - but can't seem to find anything!
Any help much appreciated.
Code:
public static XmlDocument SerializeToXML<T>(List<T> list, string rootElement)
{
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attr = new XmlAttributes();
attr.XmlRoot = new XmlRootAttribute(rootElement);
overrides.Add(typeof(List<T>), attr);
XmlDocument xmlDoc = new XmlDocument();
XPathNavigator nav = xmlDoc.CreateNavigator();
using (XmlWriter writer = nav.AppendChild())
{
XmlSerializer ser = new XmlSerializer(typeof(List<T>), overrides);
ser.Serialize(writer, list);
}
return xmlDoc;
}
Code used to test:
[TestFixture]
public class BaseTesting
{
[Test]
public void test()
{
List<ListTestClass> list = new List<ListTestClass>();
for (int i = 0; i < 20; i++)
{
list.Add(new ListTestClass() { intProp = 1, stringProp = "string1", dtProp = DateTime.Now });
}
XmlDocument doc = Beyond.Base.Util.XMLUtils.SerializeToXML<ListTestClass>(list, "root");
}
}
public class ListTestClass
{
public int intProp { get; set; }
public string stringProp { get; set; }
public DateTime dtProp { get; set; }
[XmlElement("Inner",typeof(InnerListTestClass))]
public InnerListTestClass inner { get { return new InnerListTestClass() { intProp = 1, stringProp = "string1", dtProp = DateTime.Now }; } }
}
public class InnerListTestClass
{
public int intProp { get; set; }
public string stringProp { get; set; }
public DateTime dtProp { get; set; }
}
XML Output:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
</ListTestClass>
</root>
inner
property must have setter in order to be serializable.
If you change it to
public InnerListTestClass inner { get; set; }
It will be serialized, as you expect it to.
<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T01:57:07.1200742-07:00</dtProp>
<Inner>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T01:57:07.1210743-07:00</dtProp>
</Inner>
</ListTestClass>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With