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

查看:29
本文介绍了使用 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 数据类型,如果你创建一个列,你可以简单地插入到那个列中,这里是一个 insert 和你的类似,这个表的第二列的数据类型是 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天全站免登陆