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

发布于 2024-07-05 07:28:51 字数 588 浏览 6 评论 0原文

我似乎总是在 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 吗?

请帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀お生予夺 2024-07-12 07:28:51

我最终不必使用它,但我找到了我认为最好的答案。 基本上,您加载一个 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文