使用C#将XML直接保存到数据库 [英] Save XML directly to Database with C#

查看:838
本文介绍了使用C#将XML直接保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的xml文件的一部分

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>

我在某处读到有一种方法可以将这个XML直接保存到数据库。我使用VS2010。我已经创建了数据集,对于我需要从这个XML的数据。有没有什么办法直接映射这个XML到数据集?
任何其他想法?
我还必须保存一些其他更复杂的XML文件到数据库。

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.

我试过xsd.exe为这个XML创建xsd模式。

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

推荐答案

SQL Server 2005+具有本机XML数据类型,如果创建列,则只需插入该列,类似于您的插入,此表格的第二个column's datatype is xml

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>')

这篇关于使用C#将XML直接保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆