views:

101

answers:

1

Here is a part of my xml file

<teams>
  <team-profile>
    <name>Australia</name>
    <id>1</id>
    <stats type="Test">
      <span>1877-2010</span>
      <matches>721</matches>
      <won>339</won>
      <lost>186</lost>
      <tied>2</tied>
      <draw>194</draw>
      <percentage>47.01</percentage>
    </stats>
    <squad>
      <player id="135" fullname="Shane Warne"/>
      <player id="136" fullname="Damien Martyn"/>
      <player id="138" fullname="Michael Clarke"/>
    </squad>
  </team-profile>
</team>

I have read somewhere that there is a way to save this XML directly to database. I am using VS2010. I have created the dataset, for the data i need from this xml. Is there any way to map this XML directly on dataset? Any other idea? I also have to save some other more complex XML files to database.

I have tried xsd.exe to create xsd schema for this XML.

+1  A: 

SQL Server 2005+ has a native XML datatype, if you create a column, you can simply do an insert into that column, here is an insert similar to yours, this table's second column's datatype is xml

INSERT INTO docs VALUES (1, '<book genre="security"
publicationdate="2002" ISBN="0-7356-1588-2">
   <title>Writing Secure Code</title>
   <author>
      <first-name>Michael</first-name>
      <last-name>Howard</last-name>
   </author>
   <author>
      <first-name>David</first-name>
      <last-name>LeBlanc</last-name>
   </author>
   <price>39.99</price>
</book>')
INSERT INTO docs VALUES (2, 
'<doc id="123">
    <sections>
   <section num="1"><title>XML Schema</title></section>
   <section num="3"><title>Benefits</title></section>
   <section num="4"><title>Features</title></section>
    </sections>
</doc>')
RandomNoob
Also to note, since you mentioned datasets, datasets have a writeXml property that you could use to get the underlying xml and save it that way as well via an intermediate memory stream if you don't want to write it to file.
RandomNoob
Thanks, i am going to try it.
LifeH2O
I meant writexml method sorry.
RandomNoob