xml文件格式如下<?xml version="1.0" encoding="GB2312" ?> 
 <allproducts version="1.0">
 <products>
  <product name="prodId" value="28127" /> 
  <product name="prodContent" value="5" /> 
  <product name="prodPrice" value="5" /> 
  </products>
 <products>
  <product name="prodId" value="10335" /> 
  <product name="prodContent" value="5" /> 
  <product name="prodPrice" value="4.95" /> 
  </products>
 <products>
  <product name="prodId" value="28328" /> 
  <product name="prodContent" value="5" /> 
  <product name="prodPrice" value="5" /> 
  </products>
  </allproducts>遍历代码如下            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("E:\\1.xml");
            XmlNodeList list2 = xmldoc.SelectSingleNode("/allproducts").ChildNodes;
            XmlNodeList list = xmldoc.SelectSingleNode("/allproducts/products").ChildNodes;
            //XmlNode list = xmldoc.SelectSingleNode("/allproducts/products");
            foreach( XmlNode xn1 in list2 )
            {
                foreach (XmlNode xn in list)
                {
                    //遍历子节点
                    XmlElement xe = (XmlElement)xn;//将子节点类型转换为xmlelement类型
                    if (xe.GetAttribute("name") == "prodId")
                    {
                        M_prodId = Convert.ToInt32(xe.GetAttribute("value"));
                    }
                    if(xe.GetAttribute("name") == "prodContent")
                    {
                        M_prodContent = decimal.Parse(xe.GetAttribute("value"));
                    }                }
            }这段代码的问题就是总是取第一个<products></products>中的值,跳不到下面两个,这是什么问题呢?请高手赐教,谢谢!

解决方案 »

  1.   

    补充一下,单步调试看xn1是可以循环往下取的,但是到了里面的foreach就不对了,还是第一个。
      

  2.   

       XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load("E:\\1.xml");
                XmlNodeList list = xmldoc.SelectSingleNode("//products").ChildNodes;                foreach (XmlNode xn in list)
                    {
                        //遍历子节点
                        XmlElement xe = (XmlElement)xn;//将子节点类型转换为xmlelement类型
                        if (xe.GetAttribute("name") == "prodId")
                        {
                            M_prodId = Convert.ToInt32(xe.GetAttribute("value"));
                        }
                        if(xe.GetAttribute("name") == "prodContent")
                        {
                            M_prodContent = decimal.Parse(xe.GetAttribute("value"));
                        }                }
               
      

  3.   

    SelectSingleNode???用这个不对吧?这个是搜错特定的某一个吧?应该是Select、SelectNodes吧?装VS中,无法测试,既不清了
      

  4.   

     把XmlNodeList list2 = xmldoc.SelectSingleNode("/allproducts").ChildNodes;
    改成 XmlNodeList nodelist = xmldoc.GetElementsByTagName("products");
    就可以了
      

  5.   


    XmlNodeList list2 = xmldoc.SelectSingleNode("/allproducts").ChildNodes;  foreach (XmlNode node in nodelist)
                {
                    
                        foreach (XmlNode xn in node.ChildNodes)
                        {
                            //遍历子节点
                            XmlElement xe = (XmlElement)xn;//将子节点类型转换为xmlelement类型
                        if (xe.GetAttribute("name") == "prodId")
                        {
                            M_prodId = Convert.ToInt32(xe.GetAttribute("value"));
                        }
                        if(xe.GetAttribute("name") == "prodContent")
                        {
                            M_prodContent = decimal.Parse(xe.GetAttribute("value"));
                        }                   }
                }刚刚说错了,仔细看了你的错误,应该这么改。。
      

  6.   

    把foreach中的nodelist,改成list2就行了
      

  7.   

    问题解决了,我是这样写的:
    不知道各位大虾还有更简单的方法没有呢XmlNodeList list2 = xmldoc.SelectSingleNode("/allproducts").ChildNodes;
                foreach (XmlNode xn1 in list2)
                {
                    XmlElement xe = (XmlElement)xn1;
                    XmlNodeList list = xe.ChildNodes;
                    foreach (XmlNode xn in list)
                    {
                        //遍历子节点
                        XmlElement xe2 = (XmlElement)xn;//将子节点类型转换为xmlelement类型
                        if (xe2.GetAttribute("name") == "prodId")
                        {
                            M_prodId = Convert.ToInt32(xe2.GetAttribute("value"));
                        }
                        if (xe2.GetAttribute("name") == "prodContent")
                        {
                            M_prodContent = decimal.Parse(xe2.GetAttribute("value"));
                        }                }
                }