string[] files = Directory.GetFiles(@"c:\receipts\", "*.xml");
        foreach (string file in files)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(file);
            XmlNodeList node1 = xmlDoc.SelectNodes("//User/Attribute[2]/Value");
         }    
上面的代码能取到以下XML第2个节点的值,但是能不能按 name="性别" 这样来快速查找,不要按顺序找?XML
<User>
    <Attribute>
      <Name>名字</Name>
      <Value>0</Value>
    </Attribute>
    <Attribute>
      <Name>性别</Name>
      <Value>0</Value>
    </Attribute>
    <Attribute>
      <Name>年纪</Name>
      <Value>0</Value>
    </Attribute>
    <Attribute>
      <Name>身高</Name>
      <Value>0</Value>
    </Attribute>
    <Attribute>
      <Name>邮件</Name>
      <Value>0</Value>
    </Attribute>

解决方案 »

  1.   

    GetElementByTagName("Name")取得一个NodeList 然后找  性别的那个Node
      

  2.   

    参考http://www.cnblogs.com/xiang/archive/2006/03/13/349303.html
      

  3.   

           string[] files = Directory.GetFiles(@"c:\receipts\", "*.xml");
            foreach (string file in files)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(file);            XmlNodeList node1 = xmlDoc.GetElementsByTagName("性别");
                      DataRow dr = dt.NewRow();
                foreach (XmlNode node in node1)
                {
                    string str1 = node.InnerText;
                    TextBox1.Text = str1;            }找不到内容
      

  4.   

    <User>
      <Attribute>
      <Name>名字</Name>
      <Value>0</Value>
      </Attribute>
      <Attribute>
      <Name>性别</Name>
      <Value>0</Value>
      </Attribute>
      <Attribute>
      <Name>年纪</Name>
      <Value>0</Value>
      </Attribute>
      <Attribute>
      <Name>身高</Name>
      <Value>0</Value>
      </Attribute>
      <Attribute>
      <Name>邮件</Name>
      <Value>0</Value>
      </Attribute>
    </use>有三层节点, 在线等啊,解决马上结贴
      

  5.   

    只要这行代码这样改下即可:
    XmlNodeList node1 = xmlDoc.SelectNodes("//User/Attribute[Name='性别']/Value");
      

  6.   

      XmlDocument xmlDoc = new XmlDocument();
       xmlDoc.Load(file);   XmlNodeList node1 = xmlDoc.GetElementsByTagName("Name");  DataRow dr = dt.NewRow();
       foreach (XmlNode node in node1)
       {
    foreach(XmlNode xn in node.ChindNodes)
    {
    if(xn.InnerText.Equals("性别"))
       string str1 = xn.InnerText;
       TextBox1.Text = str1;}
      

  7.   

            string xml = @"<User>
      <Attribute>
      <Name>名字</Name>
      <Value>0</Value>
      </Attribute>
      <Attribute>
      <Name>性别</Name>
      <Value>0</Value>
      </Attribute>
      <Attribute>
      <Name>年纪</Name>
      <Value>0</Value>
      </Attribute>
      <Attribute>
      <Name>身高</Name>
      <Value>0</Value>
      </Attribute>
      <Attribute>
      <Name>邮件</Name>
      <Value>0</Value>
      </Attribute></User>";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlNode node = doc.SelectSingleNode("//User/Attribute/Name[text()='性别']");