mapping (different)XML(formats) to a database table
how can i map xml file(s) of different formats to a table in my database say for example i have a table called Person
FirstName nvarchar(50),
LastName nvarchar(50),
Address nvarchar(500)
i have to map xml files submitted by users to the fields of Person
, the xml files can be in different formats say for example one user submits in the following format
<Person>
<Names>
<FirstName>john</FirstName>
<LastName>smith</LastName>
</Names>
<Names>
<FirstName>john</FirstName>
<LastName>smith</LastName>
</Names>
</Person>
and another user submits it in the following format
<Person>
<PersonalInfo>
<Names>
<FirstName>john</FirstName>
<LastName>smith</LastName>
</Names>
.
.
.
<PersonalInfo>
</Person>
how can i device a solution that will handle different cases (different xml formats), so that if a user submits a xml file in whatever format i just have to tell the format to my aplication and its values will automatically gets mapped to the said table... im using visual studio 2010 as an IDE and C#.NET, i hope have narrated the problem well enough...
please advice...
Since I don't how dynamic this should be (meaning how often you will change the mappings) or how many different XMLs do u have I will suggest you to map the table(s) to a common class using your preferred ORM and create class instances using LinqToXml (which is very easy). One method or class per XML structure.
There is a sample here for the LinqToXml -> http://www.switchonthecode.com/tutorials/introduction-to-linq-simple-xml-parsing
Also, if you don´t want to use it and are using .NET 4 I created a simple library to produce dynamic objects from a XML string. You can take a look at it here -> https://github.com/tucaz/XmlToObjectParser
you can identify the schema you are acquiring and use appropriate XSLT File to convert it to the latest version. Note: you should make something generic to identify the schema (e.g. )
(I Like 1. better) create an Software Adapter interface that changes the mappings (Like add-ins work)
精彩评论