I have a basic object model of an object that is serialized with System.Xml.XmlSerialization material. I need to use the XmlAttributeOverrides function to set the xml element names for a set of child elements.
public class Foo{
public List Bars {get; set; }
}
public class Bar {
public string Widget {get; set; }
}
using a standard XML serializer, it will look like
<Foo>
<Bars>
<Bar>...</Bar>
</Bars>
</Foo>
I need to use XmlOverrideAttributes to say:
<Foo>
<Bars>
<SomethingElse>...</SomethingElse>
</Bars>
</Foo>
but I can’t get him to rename the children in the collection ... I can rename the set itself ... I can rename the root ... not sure what I am doing wrong.
here is the code i have right now:
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
var bars = new XmlElementAttribute("SomethingElse", typeof(Bar));
var elementNames = new XmlAttributes();
elementNames.XmlElements.Add(bars);
xOver.Add(typeof(List), "Bars", elementNames);
StringBuilder stringBuilder = new StringBuilder();
StringWriter writer = new StringWriter(stringBuilder);
XmlSerializer serializer = new XmlSerializer(typeof(Foo), xOver);
serializer.Serialize(writer, someFooInstance);
string xml = stringBuilder.ToString();
but that doesn't change the name of the element at all ... what am I doing wrong?
thank