使用C#读取存储在Sql Server中的Xml列数据 [英] Read Xml column data stored in Sql Server using C#

查看:121
本文介绍了使用C#读取存储在Sql Server中的Xml列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我将xml数据存储在SQLserver表的一列中。

如何访问数据及其元素使用c#代码?





提前致谢..



I have xml data stored in a column of SQLserver table.
How do i access the data,its elements using c# code?


Thanks in advance..

推荐答案

通常,XML存储在Unicode字符串中,该字符串通过ADO.NET映射到.NET System.String 。然后,您可以将此字符串解析为XML。所有XML解析器都可以从任何输入流进行解析。如果您有一个表示XML内容的字符串(不是XML文件,就像在其他情况下那样),您可以将流数据放在 System.IO.MemoryStream 的实例中:

http://msdn.microsoft.com/en-us/ library / system.io.memorystream.aspx [ ^ ]。



如果您有字符串,可以找到有关如何使用流的简短代码示例: http://stackoverflow.com/questions/8143732/stringstream-in-c-sharp [<一个href =http://stackoverflow.com/questions/8143732/stringstream-in-c-sharptarget =_ blanktitle =New Window> ^ ]。



如果由于某种原因,您将XML存储为二进制数据(使用BLOB数据类型),您仍然可以使用内存流,您可以使用以o形式初始化的原始数据f字节数组,您可以使用ADO.NET DataReader 获取。请参阅此Microsoft文章: http://msdn.microsoft.com/en-us/library/87z0hy49。 aspx [ ^ ]。



现在,您需要使用一个XML解析器,从流初始化它并解析它。选择什么类型的解析器?这是我对它们的简短概述:

Normally, XML is stored in a Unicode string which is mapped via ADO.NET to .NET System.String. Then you can parse this string as XML. All XML parser can parse from any input stream. If you have a string representing XML content (not XML file, as in other cases), you can put the stream data in an instance of System.IO.MemoryStream:
http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx[^].

You can find a short code sample on how to work with the stream if you have a string: http://stackoverflow.com/questions/8143732/stringstream-in-c-sharp[^].

If, by some reason, you store XML as binary data (using BLOB data type), you still can use memory stream, which you initialize with raw data in the form of array of bytes, which you can obtain using ADO.NET DataReader. Please see this Microsoft article: http://msdn.microsoft.com/en-us/library/87z0hy49.aspx[^].

Now, you need to take one of XML parsers, initialize it from stream and parse it. What type of parser to choose? This is my short overview of them:
  1. 使用 System.Xml.XmlDocument 类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。
  2. 使用类 System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。
  3. 使用类 System.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。
  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].





祝你好运,

-SA


这篇关于使用C#读取存储在Sql Server中的Xml列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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