<?xml version="1.0" encoding="utf-8" ?>
<UserConment>
  <News>
    <ID>1786</ID>
    <Conment>这个很好
    </Conment>
  </News>
  <News>
    <ID>1968</ID>
    <Conment>
      我认为这个还有待改进
    </Conment>
  </News>
  <News>
    <ID>1786</ID>
    <Conment>
      这个不行 效果不好
    </Conment>
  </News>
  ... ... ...
  ... ... ...
</UserConment>
我要根据ID号读取某个ID的所有结点信息,这个XML文件数据量很大,采用什么什么方式?XmlReader只能读,XmlDocument使用XPath很慢,那么采用什么解决方法好呢?

解决方案 »

  1.   


      XmlDocument document = new XmlDocument();
                document.Load(@"path");
                XmlNode basicnode = document.DocumentElement;
                if (basicnode.Name == "UserConment")
                {
                    foreach (XmlNode node in basicnode.ChildNodes)
                    {
                        if (node.Name == "News")
                        {
                            foreach (XmlNode cnode in node.ChildNodes)
                            {
                                if (cnode.Name == "Id" && cnode.InnerText == "1786")
                                {
                                    ID = cnode.InnerText;
                                }
                                if (cnode.Name == "Conment")
                                {
                                    Conment = cnode.InnerText;
                                }
                            }
                        }
                    }            }
    写同样.   if (cnode.Name == "Id" && cnode.InnerText == "1786")
      {
          cnode.InnerText = ID;
      }
    在后面加个.
      document.Save(path)就可以了.
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml; namespace WindowsApplication211
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            XmlDocument Doc = new XmlDocument();
                Doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
                    + "<UserConment>"
                    + "<News>"
                    + "<ID>1786</ID>"
                    + "<Conment>这个很好"
                    + "</Conment>"
                    + "</News>"
                    + "<News>"
                    + "<ID>1968</ID>"
                    + "<Conment>"
                    + "我认为这个还有待改进"
                    + "</Conment>"
                    + "</News>"
                    + "</UserConment>");            XmlNode Node=Doc.SelectSingleNode("//UserConment/News/ID[.=\"1786\"]");
                MessageBox.Show(Node.ChildNodes[0].InnerText);  
            }
        }
    }
      

  3.   

    修改下:
     foreach (XmlNode cnode in node.ChildNodes)
    {
           if (cnode.Name == "Id" && cnode.InnerText != "1786")
         {
          continue;
         }
        if(code.Name="Id")
        {
          ID = cnode.InnerText;    }
        if (cnode.Name == "Conment")
         {
           Conment = cnode.InnerText;
         }
     }
      

  4.   

    如果是.net3.5的,用Linq好了, var result1 = from p in doc.Descendants("News")
                              .Where(u => u.Element("ID").Value == "1723")
                              select p;
                XElement xel1 = result.FirstOrDefault();
      

  5.   

    用的是2.0  XmlDocument对于大数据量的查询 很慢啊
      

  6.   

    为啥不放到DB里啊 数据量大的话还是DB好 没有办法的话也就只能是XPath了