首先我的XML文档如下:
<Root> <RuleExecutionLog>
   <Messages xmlns="http://www.inrule.com/XmlSchema/Schema">
      <RuleChange ElementStateId="Row1" Value="false"/>
      <RuleChange ElementStateId="Row2" Value="false"/>
      ....
   </Messages>
 </RuleExecutionLog> <RuleExecutionLog>
   <Messages xmlns="http://www.inrule.com/XmlSchema/Schema">
      <RuleChange ElementStateId="Row3" Value="false"/>
      <RuleChange ElementStateId="Row4" Value="false"/>
      ...
   </Messages>
 </RuleExecutionLog></Root>
-----------------------------------------------------------------现在要遍历,取出RuleChange节点的ElementStateId的值  我的代码是这样写的 但是每次提取出来的值"Row1" "Row2" "Row1" "Row2" 
也就是取了两遍第一个<RuleExecutionLog>下面的值 而没有走到第二个
代码如下: 
XmlNamespaceManager spaceManager = new XmlNamespaceManager(doc.NameTable);  //doc就是以上的xml document
spaceManager.AddNamespace("XX", "http://www.inrule.com/XmlSchema/Schema");  //添加命名空间XmlNodeList rootlist = doc.SelectSingleNode("Root").ChildNodes;    //取出Root根节点下面的所有节点(这里面我理解就是两个RuleExecutionLog节点吧)
foreach (XmlNode rootxn in rootlist)         //开始遍历那连个RuleExecutionLog节点
{
    XmlElement rootxe = (XmlElement)rootxn;                      
    XmlNodeList nodelist = rootxn.SelectSingleNode("//XX:Messages", spaceManager).ChildNodes; //取出RuleExecutionLog的Messages节点
    foreach (XmlNode xn in nodelist) //遍历这个Messages节点下面的
    {
           XmlElement xe = (XmlElement)xn;
           if (xe.Name == "RuleChange")         //如果节点是RuleChange
           {
                 listBox1.Items.Add(xe.Attributes["ElementStateId"].Value);     //显示ElementStateId属性的值
                                        
           }
                                       }
}请问我什么地方写的有问题呀,我想显示的是:"Row1" "Row2" "Row3" "Row4"  请大虾帮我挑挑错在哪儿吧  谢谢~~~~ 

解决方案 »

  1.   

    http://www.cnblogs.com/wowojo/archive/2008/07/20/1247075.html
      

  2.   

    XmlDocument doc = new XmlDocument();
    doc.Load("Demo.xml");
    XmlNamespaceManager spaceManager = new XmlNamespaceManager(doc.NameTable); 
    spaceManager.AddNamespace("XX", "http://www.inrule.com/XmlSchema/Schema");  XmlNodeList rootlist = doc.SelectNodes("Root//RuleExecutionLog//XX:Messages", spaceManager);   
    foreach (XmlNode rootxn in rootlist)       
    {
        XmlElement xe = (XmlElement)rootxn;
        foreach (XmlNode xn in xe.ChildNodes)
        {
            if (xn.Name == "RuleChange")     
            {
                MessageBox.Show( (xn as XmlElement).GetAttribute("ElementStateId"));     
            }                    
        }
    }
      

  3.   

    XmlDocument doc=new XmlDocument();
    doc.LoadXml(XMLstring);
    XmlNamespaceManager spaceManager = new XmlNamespaceManager(doc.NameTable);  //doc就是以上的xml document 
    spaceManager.AddNamespace("XX", "http://www.inrule.com/XmlSchema/Schema");  //添加命名空间 
    XmlNode node = doc.SelectSingleNode("Root") ;  //取出Root根节点下面的所有节点(这里面我理解就是两个RuleExecutionLog节点吧)  //Console.WriteLine(rootxn.InnerXml);
        
        XmlNodeList nodelist = node.SelectNodes("//XX:Messages", spaceManager) ; //取出RuleExecutionLog的Messages节点 
        //Console.WriteLine(nodelist[1].InnerXml);
        foreach (XmlNode xn in nodelist) //遍历这个Messages节点下面的
        { 
         XmlNodeList nodes=xn.ChildNodes;
         foreach(XmlNode node1 in nodes)
         {
         Console.WriteLine(node1.Attributes[0].InnerText);
         }
        }