将 C# 2.0 System.Data.SqlTypes.SqlXml 对象转换为 System.Xml.XmlNode

编程入门 行业动态 更新时间:2024-10-10 15:26:39
本文介绍了将 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?bytes/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? bytes/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

本文发布于:2023-11-11 08:21:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   对象   Data   System   SqlTypes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!