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?
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: