Patrick Desjardins
Patrick Desjardins

Reputation: 140923

External Xml to C# Object, what is the easiest way?

We have XML generated from an external program that needs to be read into our C# application.

I know we could use C# classes with [Serializable] tag to load the into objects but the problem is that we already have our object that is really more OO divised than the Xml. We do not want to have 2 sets of objects.

My though is that I could open the file with LinqToXml and fill up our objects. Is it okay to do it this way or the real best way is to use the [Serializable] tag with XmlElementAttribute and other tag into our OO model? (If the latest is the good solution, how can I put an attribute in object inside my object or to work with inheritance?).

Edit : Example

Let say that I have :

public class Movie {
    public string Name;
    public Actor LeadActor;
}

public class Actor {
    public string Name;
    public DateTime DOB;
}

And I want to serialize the Movie like this:

<Movie>
    <Name>Casino Royale</Name>
    <LeadActor>Daniel Craig</LeadActor>
</Movie>

The only way I see to make it works is to modify the Movie to have a LeadActor property that will concatenare the Actor property and make both Actor property with [XmlIgnore] attribute. But what if I do not want to alter those business object?

Upvotes: 0

Views: 736

Answers (4)

Polyfun
Polyfun

Reputation: 9639

You could use an xslt transformation to transform the source xml into the xml format that matches your object model, and then directly deserialize the transformed xml into objects.

Upvotes: 0

Shaun Bowe
Shaun Bowe

Reputation: 10008

You could implement IXmlSerializable on your top level object to do some custom serialization.

Upvotes: 0

Lukas
Lukas

Reputation: 2352

If the structure of the xml data is known (and not too complex) I find using Xml.Linq the easiest way. Each class contains public static method to reconstruct itself from xml. You have to do it manually, but if the xml structure does not correspond to object structure that seems like the only way - and this one is quite simple.
For your example it would be:

public class Movie {
    public string Name;
    public Actor LeadActor;
    public static Movie FromXml (XElement x)
    {
        Movie m = new Movie();
        m.Name = x.Element("Name").Value;
        m.LeadActor = Actor.FromXml(x.Element("LeadActor"));
        return m;
    }
}

public class Actor {
    public string Name;
    public DateTime DOB;
    public static Actor FromXml (XElement x)
    {
        Actor a = new Actor();
        a.Name = x.Value;
        a.DOB = ... 
        return a;
    }
}

Upvotes: 2

alexm
alexm

Reputation: 6882

[Serializable] attribute enabled binary serialization, not Xml serialization. To support XML serialization object must have default constructor and public settable properties. If your object model meets this requirement I would consider using built-it XML serialization - it saves time in the long run.

Upvotes: 1

Related Questions