【发布时间】:2012-05-22 19:26:17
【问题描述】:
我有三个表 table1, table2, table3 与 col1, col2 和身份 ID 列。这些表关系是在数据库中定义的。
我正在尝试创建一个接受 xml 字符串输入并将该数据保存到表中的存储过程。
这是 XML 输入
<root>
<table1 col1='a' col2='b'>
<table2Array>
<table2 col1='c' col2='d'>
<table3array>
<table3 col1='g' col2='h' />
<table3 col1='i' col2='j' />
</table3array>
</table2>
<table2 col1='c' col2='d'>
<table3array>
<table3 col1='k' col2='l' />
<table3 col1='i' col2='j' />
</table3array>
</table2>
</table2Array>
</table1>
<table1 col1='a' col2='b'>
<table2Array>
<table2 col1='e' col2='f'>
<table3array>
<table3 col1='i' col2='j' />
<table3 col1='i' col2='j' />
</table3array>
</table2>
<table2 col1='e' col2='f'>
<table3array>
<table3 col1='g' col2='h' />
<table3 col1='g' col2='h' />
</table3array>
</table2>
</table2Array>
</table1>
</root>
此 xml 来自第三方对象,我们无法控制修改第三方对象以发出不同格式的 xml。
算法:
- 循环遍历每个节点
- 将节点属性插入表中
- 获取最后一个标识值
- 以最后一个标识值作为外键调用子节点
- 直到没有子节点为止
这是处理这种情况的唯一方法吗?如果是,如何遍历 xml 节点?
请帮忙!!
谢谢,
埃森
【问题讨论】:
-
回答遍历 xml 节点,您可以使用
XmlTextReader从文件/流中读取,XmlNodeReader从XmlElement实例中读取。我建议这样做而不是尝试在 SQL Server 中处理 XML。 -
您使用什么版本的 SQL Server?
-
Sql server 2008。我刚刚发布了自己的答案。如果您确实有不同的处理方式,请发布您的解决方案。谢谢
标签: c# .net sql-server sql-server-2008