One option is to use XmlAnyElementAttribute as below:
public class Foo
{
public string Name { get; set; }
public Bar Bar { get; set; }
[XmlAnyElementAttribute]
public XmlElement[] BarElements
{
get { return null; }
set
{
Bar = new Bar();
var barType = Bar.GetType();
foreach (var prop in value)
barType.GetProperty(prop.Name.Substring(4)).SetValue(Bar, prop.InnerText);
}
}
}
public class Bar
{
public string Data { get; set; }
public string MoreData { get; set; }
}
When XmlSerializer does not recognize an element it adds it to property of type XmlElement[] marked by XmlAnyElementAttribute. That is where you can process Bar properties. I used reflection there to show the idea.
Another option is to deserialize twice and connect Bar with Foo:
public class Foo
{
public string Name { get; set; }
public Bar Bar { get; set; }
}
[XmlRoot("Foo")]
public class Bar
{
[XmlElement("Bar_Data")]
public string Data { get; set; }
[XmlElement("Bar_MoreData")]
public string MoreData { get; set; }
}
var foo = (Foo) new XmlSerializer(typeof (Foo)).Deserialize(...);
var bar = (Bar) new XmlSerializer(typeof (Bar)).Deserialize(...);
foo.Bar = bar
Yet another option with no intrusion to classes being deserialized:
public class Foo
{
public string Name { get; set; }
public Bar Bar { get; set; }
}
public class Bar
{
public string Data { get; set; }
public string MoreData { get; set; }
}
var fooSerializer = new XmlSerializer(typeof (Foo));
fooSerializer.UnknownElement += (sender, e) =>
{
var foo = (Foo) e.ObjectBeingDeserialized;
if(foo.Bar == null)
foo.Bar = new Bar();
var propName = e.Element.Name.Substring(4);
typeof(Bar).GetProperty(propName).SetValue(foo.Bar, e.Element.InnerText);
};
var fooInstance = fooSerializer.Deserialize(...);
and if double deserialization or reflection is problematic performance wise, then you could create a surrogate proxy class:
[XmlRoot("Foo")]
public class FooSurrogate
{
public string Name { get; set; }
public string Bar_Data { get; set; }
public string Bar_MoreData { get; set; }
public Foo ToFoo()
{
return new Foo
{
Name = Name,
Bar = new Bar
{
Data = Bar_Data,
MoreData = Bar_MoreData
}
};
}
}
var seializer = new XmlSerializer(typeof (FooSurrogate));
var foo = ((FooSurrogate) seializer.Deserialize(...)).ToFoo();