1

I get an xml file with a lot of data from another software system. I have to do a lot of work with this data, so i would like to transform this xml data into a specific domain model, which allows me to work more efficient. So i need a mechanism to map the xml data to the domain model and vice versa. What are my possibilities, can you complete the list?

  • Linq to XSD
  • Manual mapping over linq to xml oder xml DOM
  • DataSets (Read/Write XML) !?
  • ...?

Thank you in advance, best regards :-) Laurin

Edit: Simply said: I want to do OR Mapping but instead of an DBMS i have an XML file ;-)

Laurin
  • 13
  • 4
  • 1
    This is a useful tool which may help although I'm not sure exactly what it is you need to do with this xml file. http://xsd2code.codeplex.com/ – pmcilreavy Apr 01 '11 at 07:50
  • 1
    Xsd2Code will work in two steps - first you'll need to use xsd.exe to generate a schema for the xml (assuming you don't have one already), then use Xsd2Code to generate the domain models from the schema. Xsd2Code has a command line switch which can add serialize/deserialize methods to the model classes, which you can use to load the data.. disclaimer: Linq2Xml is probably a simpler solution though :) – MattDavey Apr 01 '11 at 08:34

3 Answers3

3

try to use Linq to Xml. Your mapper will be looks like following code.

xml:

<contacts>
  <contact contactId="2">
     <firstName>Barry</firstName>
     <lastName>Gottshall</lastName>
  </contact>
  <contact contactId="3">
     <firstName>Armando</firstName>
     <lastName>Valdes</lastName>
  </contact>
</contacts>

code for load data:

XDocument loaded = XDocument.Load(@"C:\contacts.xml");

mapping:

List<MyContact> contacts = (from c in loaded.Descendants("contact")
    select new MyContacts() {
                                 FirstName = (string)c.Element("firstName"),
                                 LastName = (string)c.Element("lastName")
                             }).ToList();
Andrei Andrushkevich
  • 9,905
  • 4
  • 31
  • 42
0

You could also implement System.Xml.Serialization.IXmlSerializable assuming that there is a one to one relationship between xml models and your objects. There is more information at Proper way to implement IXmlSerializable?

Community
  • 1
  • 1
detaylor
  • 7,112
  • 1
  • 27
  • 46
  • This solution should be less code for the simple case, but I think it creates too much coupling between the objects and one specific way of physically representing them. E.g. if you instead use the Data Mapper pattern, you could also use alternative "back end" formats. That would also deal more cleanly with migrating from one version of the saved data to a new version (although I imagine this could also be done by overriding Serialize and Deserialize?). – Jon Coombs Oct 19 '13 at 06:18
0

XML Serialization / De-Serialization seems a good idea, if you have a domain object available.

More info about XmlSerializer Class

Anuraj
  • 18,859
  • 7
  • 53
  • 79
  • As I mentioned on my comment on a different answer, I suspect that this solution is too highly coupled, compared to the Data Mapper pattern. – Jon Coombs Oct 19 '13 at 06:19