当前位置:编程学习 > C#/ASP.NET >>

C#如何读取SQL Server中xml字段内容?


SQL Server中xml类型的字段中存储的内容如下:
<Test>
<X>
   <D>1</D>
   <D>2</D>
   <D>3</D>
</X>

</Test>
我想把XML中存储的D的三个值1,2,3取出来放在数组或者list里面,如:{1,2,3},该怎么写代码? --------------------编程问答-------------------- 参考
http://topic.csdn.net/u/20101027/13/dcb500a7-3e23-4347-8e03-0b02e3761d12.html --------------------编程问答-------------------- 查询出xml字段出的值给string xmlStr,然后
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.LoadXml(xmlStr);
XmlNodeList nodes=xmlDoc.SelectNodes(@"/Test/X/D");
List<string> values=new List<string>();
foreach(XmlNode node in nodes)
    values.Add(node.InnerText);

--------------------编程问答-------------------- 谢谢,照着你说的我已经实现了我的需求,我还有个问题,不知道对于这个xmlStr有没有最大长度的限制?我xml中存放的数据可能有几十万个,放在数据库字段中是肯定没有问题,从数据库中读出放在xmlStr中时候会不会长度过大,导致xmlStr放不下?如果有这种情况,怎么解决?
引用 2 楼  的回复:
查询出xml字段出的值给string xmlStr,然后
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.LoadXml(xmlStr);
XmlNodeList nodes=xmlDoc.SelectNodes(@"/Test/X/D");
List<string> values=new List<string>();
foreach(Xml……
--------------------编程问答-------------------- 太大,可能会导致你的程序崩溃,对于超大文件,你可能需要用其他方法了,比如内存映射,一部分一部分读取,但是需要你自己去解析内容了

或者用 XmlTextReader http://msdn.microsoft.com/zh-cn/library/system.xml.xmltextreader(v=vs.80).aspx --------------------编程问答-------------------- 太大会报outofmemory 异常。

XmlDocument是以DOM方式解析xml. DOM参考:http://en.wikipedia.org/wiki/Document_Object_Model

可以使用XmlTextReader (类似SAX方式,但不是SAX. 参考:
Comparing XmlReader to SAX Reader
http://msdn.microsoft.com/en-us/library/sbw89de7(v=vs.71).aspx): 

SAX参考: http://en.wikipedia.org/wiki/Simple_API_for_XML

XmlTextReader 使用参考:
http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader(v=vs.100)

XmlTextReader例子参考:
http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.value

--------------------编程问答-------------------- 另外XmlTextReader 应该比XmlDocument效率要高。

缺点是non-cached, forward-only access --------------------编程问答--------------------
引用 4 楼  的回复:
太大,可能会导致你的程序崩溃,对于超大文件,你可能需要用其他方法了,比如内存映射,一部分一部分读取,但是需要你自己去解析内容了

或者用 XmlTextReader http://msdn.microsoft.com/zh-cn/library/system.xml.xmltextreader(v=vs.80).aspx


引用 1 楼  的回复:
参考
http://topic.csdn.net/u/20101027/13/dcb500a7-3e23-4347-8e03-0b02e3761d12.html


各位coder才是真正的coder啊 

要么很晚都还在搞这些 要么很早就来了 --------------------编程问答-------------------- 如果用XmlTextReader ,我内容是存在数据库中XML字段中的,怎么使用XmlTextReader ,从数据库中取出xml生成XML文件再用XmlTextReader ?
引用 6 楼  的回复:
另外XmlTextReader 应该比XmlDocument效率要高。

缺点是non-cached, forward-only access
--------------------编程问答--------------------
引用 8 楼  的回复:
如果用XmlTextReader ,我内容是存在数据库中XML字段中的,怎么使用XmlTextReader ,从数据库中取出xml生成XML文件再用XmlTextReader ?引用 6 楼  的回复:

另外XmlTextReader 应该比XmlDocument效率要高。

缺点是non-cached, forward-only access


数据库中读出的是string吧


string szInputXml = "<TestDataXml><DataName>testing</DataName></TestDataXml>";
XmlTextReader reader = new XmlTextReader( new System.IO.StringReader( szInputXml ) );
reader.Read();
string inner = reader.ReadInnerXml();


代码来自:
http://stackoverflow.com/questions/4601139/how-to-read-a-xml-string-into-xmltextreader-type --------------------编程问答--------------------

DECLARE @x XML
SET @x = CONVERT(XML,'<items><item id="' + REPLACE(@BrandID, ',', '"/><item id="') + '"/></items>')
SELECT T.item.value('@id[1]','INT') AS link from  @x.nodes('//items/item') T(item)
--------------------编程问答-------------------- 如果XML内容为
<Test>
<X>
  <D>1</D>
  <D>2</D>
  <D>3</D>
</X>
<Y>
  <D>4</D>
  <D>5</D>
  <D>6</D>
</Y>
</Test>

用如下代码
while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element&& reader.LocalName == "D")
                {                   
                    list.Add(reader.ReadString().ToString());
                }
            }

最后会把所有<D>的值都取出来1,2,3,4,5,6.我现在如果只取<X>中<D>的值(1,2,3),该如何修改代码?


引用 6 楼  的回复:
另外XmlTextReader 应该比XmlDocument效率要高。

缺点是non-cached, forward-only access
--------------------编程问答--------------------

            string xmlString = @"<Test>
<X>
  <D>1</D>
  <D>2</D>
  <D>3</D>
</X>
<Y>
  <D>4</D>
  <D>5</D>
  <D>6</D>
</Y>
</Test>";

            XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(xmlString));
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.LocalName.Equals("Y"))
                    {
                        reader.Skip();
                    }
                    
                    if (reader.LocalName.Equals("D"))
                    {
                        Console.WriteLine(reader.ReadString().ToString());
                    }

                    
                    
                }
            }
--------------------编程问答--------------------
引用 8 楼  的回复:
如果用XmlTextReader ,我内容是存在数据库中XML字段中的,怎么使用XmlTextReader ,从数据库中取出xml生成XML文件再用XmlTextReader ?引用 6 楼  的回复:

另外XmlTextReader 应该比XmlDocument效率要高。

缺点是non-cached, forward-only access

你看了我1楼给的链接了吗? --------------------编程问答-------------------- 学习的路过我 --------------------编程问答-------------------- dreamweaver果断用不来啊!网页这一块接触的少。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,