If I have a xml file that looks like
<Foo>
<Name>Some Data</Name>
<Bar_Data>Other Data</Bar_Data>
<Bar_MoreData>More Data</Bar_MoreData>
</Foo>
And I want to turn it in to a C# class that looks like
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; }
}
Is there any way to accomplish this with just simple data annotations (XmlRoot, XmlElement, etc.) or is my only option to implement IXmlSerializable?
EDIT: Note, I only ever need to deserialize the data. I get the XML from a 3rd party source and I do not need to ever serialize Foo back in to XML (if that makes it easier).
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();
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