gpt4 book ai didi

c# - 使用 C# 将 XML 直接保存到数据库

转载 作者:搜寻专家 更新时间:2023-10-30 22:21:34 24 4
gpt4 key购买 nike

这是我的xml文件的一部分

<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 文件保存到数据库中。

我已尝试使用 xsd.exe 为此 XML 创建 xsd 模式。

最佳答案

SQL Server 2005+ 有一个原生的 XML 数据类型,如果你创建一个列,你可以简单地插入到那个列,这里是一个 insert和你的类似,这个表的第二列的数据类型是 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# - 使用 C# 将 XML 直接保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2991810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com