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},该怎么写代码?

解决方案 »

  1.   

    参考
    http://topic.csdn.net/u/20101027/13/dcb500a7-3e23-4347-8e03-0b02e3761d12.html
      

  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(XmlNode node in nodes)
        values.Add(node.InnerText);
      

  3.   

    谢谢,照着你说的我已经实现了我的需求,我还有个问题,不知道对于这个xmlStr有没有最大长度的限制?我xml中存放的数据可能有几十万个,放在数据库字段中是肯定没有问题,从数据库中读出放在xmlStr中时候会不会长度过大,导致xmlStr放不下?如果有这种情况,怎么解决?
      

  4.   

    太大,可能会导致你的程序崩溃,对于超大文件,你可能需要用其他方法了,比如内存映射,一部分一部分读取,但是需要你自己去解析内容了或者用 XmlTextReader http://msdn.microsoft.com/zh-cn/library/system.xml.xmltextreader(v=vs.80).aspx
      

  5.   

    太大会报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_XMLXmlTextReader 使用参考:
    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
      

  6.   

    另外XmlTextReader 应该比XmlDocument效率要高。缺点是non-cached, forward-only access
      

  7.   

    各位coder才是真正的coder啊 要么很晚都还在搞这些 要么很早就来了
      

  8.   

    如果用XmlTextReader ,我内容是存在数据库中XML字段中的,怎么使用XmlTextReader ,从数据库中取出xml生成XML文件再用XmlTextReader ?
      

  9.   


    数据库中读出的是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
      

  10.   


    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)
      

  11.   

    如果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),该如何修改代码?
      

  12.   


                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());
                        }                    
                        
                    }
                }
      

  13.   

    dreamweaver果断用不来啊!网页这一块接触的少。