将 C# 2.0 System.Data.SqlTypes.SqlXml 对象转换为 System.Xml.XmlNode [英] Convert C# 2.0 System.Data.SqlTypes.SqlXml object into a System.Xml.XmlNode

查看:30
本文介绍了将 C# 2.0 System.Data.SqlTypes.SqlXml 对象转换为 System.Xml.XmlNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎总是在 C# 中将数据与 XML 相互转换时遇到问题.它总是希望您创建一个完整的 XMLDocument 对象,即使您认为不应该这样做.在这种情况下,我在 MS SQL 2005 服务器中有一个 SQLXML 列,我试图将其拉出并推送到需要 XMLNode 作为参数的函数中.您会认为这很容易,但除了将其转换为字符串并创建新的 XMLNode 对象之外,我无法找到正确的方法.

我可以使用 SqlDataReader、sqlComm.ExecuteReader() 来加载读取器,并使用 sqlReader.GetSqlXml(0) 来获取 SQLXML 对象,但是如何将其转换为 XmlNode?

相反,我可以使用 sqlComm.ExecuteXmlReader() 来获取 XmlReader,但是如何从阅读器中提取 XmlNode?http://bytes.com/forum/thread177004.html 说它不能用XmlTextReader,我应该使用 XmlNodeReader 吗?

请帮忙!

解决方案

我最终不必使用它,但我找到了我认为最好的答案.基本上,您加载一个 XmlReader,从读取器创建一个 XmlDocument,然后从文档中选择一个节点列表到一个 XmnLodeList,您可以在 ForEach 语句中使用它.下面是一些示例代码:

System.Xml.XmlReader sqlXMLReader = sqlComm.ExecuteXmlReader();System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();xmlDoc.Load(sqlXMLReader);System.Xml.XmlNodeList xnlJobs = xmlDoc.SelectNodes("/job");

仍然令人费解,但至少没有从 xml 到字符串到 xml 的转换.

I seem to always have problems with converting data to and from XML in C#. It always wants you to create a full XMLDocument object even when you think you shouldn't have to. In this case I have a SQLXML column in a MS SQL 2005 server that I am trying to pull out and push into a function that requires an XMLNode as a parameter. You would think this would be easy, but outside of converting it to a string and creating a new XMLNode object I cannot figure out the right way to do it.

I can use an SqlDataReader, the sqlComm.ExecuteReader() to load the reader, and sqlReader.GetSqlXml(0) to get the SQLXML object,but then how do I convert it to an XmlNode?

Conversely I can use the sqlComm.ExecuteXmlReader() to get an XmlReader, but how do I extract a XmlNode from the reader? http://bytes.com/forum/thread177004.html says it cannot be done with a XmlTextReader, should I use a XmlNodeReader?

Help please!

解决方案

I ended up not having to use it, but I found what I think is the best answer. Basically you load an XmlReader, create an XmlDocument from the reader, then select a list of nodes from the document into an XmnLodeList, which you can use in a ForEach statement. Here is some sample code:

System.Xml.XmlReader sqlXMLReader = sqlComm.ExecuteXmlReader();
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(sqlXMLReader);
System.Xml.XmlNodeList xnlJobs = xmlDoc.SelectNodes("/job");

Still convoluted as hell, but at least there are no xml to string to xml conversions.

这篇关于将 C# 2.0 System.Data.SqlTypes.SqlXml 对象转换为 System.Xml.XmlNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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