将 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 吗?
请帮助!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终不必使用它,但我找到了我认为最好的答案。 基本上,您加载一个 XmlReader,从阅读器创建一个 XmlDocument,然后从文档中选择一个节点列表到 XmnLodeList 中,您可以在 ForEach 语句中使用它。 下面是一些示例代码:
仍然很复杂,但至少没有 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:
Still convoluted as hell, but at least there are no xml to string to xml conversions.