0

I have requirements of importing XMLs in to database and after using an application some processing will happen on these data and again XML needs to be generated using these updated values.

I have XSD for the xml which needs to be imported and generated. XSD for both kind of XMLs will remain same.

So please suggest me the best possible way to do it.

Book.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="title" type="xs:string" />
                        <xs:element name="author">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element minOccurs="0" name="name" type="xs:string" />
                                    <xs:element minOccurs="0" name="first-name" type="xs:string" />
                                    <xs:element minOccurs="0" name="last-name" type="xs:string" />
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                        <xs:element name="price" type="xs:decimal" />
                    </xs:sequence>
                    <xs:attribute name="genre" type="xs:string" use="required" />
                    <xs:attribute name="publicationdate" type="xs:date" use="required" />
                    <xs:attribute name="ISBN" type="xs:string" use="required" />
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

Book.xml (which is imported)

<?xml version="1.0" encoding="utf-8"?>
<bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.contoso.com/books">
  <book genre="Religious" publicationdate="2013-02-13" ISBN="SampleISBN">
    <title>Live and Let Live</title>
     <author>
      <name>Donald Allen</name>
      <first-name>Allen</first-name>
      <last-name>Donald</last-name>
    </author>
    <price>150</price>
  </book>
</bookstore>

Now suppose after doing some processing on above xml data, price of particular book is updated to 180 and now If I again export this as xml then updated price should come as below sample XML

Book.xml (which needs to be generated)

<?xml version="1.0" encoding="utf-8"?>
<bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.contoso.com/books">
  <book genre="Religious" publicationdate="2013-02-13" ISBN="SampleISBN">
    <title>Live and Let Live</title>
     <author>
      <name>Donald Allen</name>
      <first-name>Allen</first-name>
      <last-name>Donald</last-name>
    </author>
    <price>180</price>
  </book>
</bookstore>
  • I have just used these xml code snippet for sample pupose, actual XSD and XMLs are different altogether and very huge also. So please suggest me the path forward. – psethia Feb 14 '13 at 07:22

1 Answers1

0

You have 2 main paths:

  1. Use the XSD to create C# classes using xsd.exe, then Use XmlSerializer to "transform" the xml to and from .NET objects. See documentation

    xsd.exe yourxsd.xsd /classes

  2. Or use LINQ to Xml to read the Xml, do updates and write the Xml back to store. (XSD not really needed) Documentation

If you only update the price I'd suggest 2, if you need to access more of the fields then 1. Combinations are also possible.

Tommy Grovnes
  • 4,126
  • 2
  • 25
  • 40
  • Thanks Tommy for your quick help. As per your suggestions I am using 1st option to generate XMLs but ♣I have doubts on how to import XML to database? ♣Do I need to use XSD while importing it to database? ♣I have not finalized the DB schema yet so can we generate DB schema using XSD file? – psethia Feb 14 '13 at 10:23
  • Depends on your requirements, the size of the XML and the database. If you just want to store the XML as-is, then just store it as a single string (varchar2, clob etc) in one column of a table. If you need to manipulate or query the different book entities directly in the database then I would not store it as XML. – Tommy Grovnes Feb 14 '13 at 10:41
  • I dont need to store XML as-is but want to store values of XML. I generated the class using XSD.exe and this class file contains more than 60 classes. So in short I just want to import values of each and every elements and their attributes and is there any way to generate db schema using xsd file? – psethia Feb 14 '13 at 10:54
  • You could look at Code-first Entity framework and use the generated C# classes as your model classes. This way EF will create the database for you. http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx – Tommy Grovnes Feb 14 '13 at 11:02
  • We are bound to use .Net Framework 2.0 only. Sorry for late informing but its mandatory to implement this requirement with .Net Framework 2.0 and SQL server 2005 only. – psethia Feb 14 '13 at 11:20
  • There are some suggestions over here -> http://stackoverflow.com/questions/138575/how-can-i-create-database-tables-from-xsd-files – Tommy Grovnes Feb 14 '13 at 12:29