Nested classes deserialized from flat XML

If I have an 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 include it in 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 a way to do this with simple data annotations ( XmlRoot

, XmlElement

etc.), or is this my only possibility to implement IXmlSerializable

?

EDIT . Note. I only need to deserialize the data. I am getting XML from a third party source and I don't need to ever serialize Foo

back to XML (if it gets easier).

+3


source to share


1 answer


One option is to use the XmlAnyElementAttribute as shown 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 the XmlSerializer does not recognize an element, it adds it to a property of type XmlElement [] marked with the XmlAnyElementAttribute. This is where you can handle the Bar properties. I used reflection there to show this 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

      



Another option without interfering with the deserialization classes:

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 reflecting problematic performance is wise, then you can 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();

      

+4


source







All Articles