strxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<ResultList>
<Result>
<ErrorID>-1</ErrorID>
<Description>系统处理单据出错</Description>
<PosCount>1</PosCount>
</Result>
<Result>
<ErrorID>10302</ErrorID>
<Description>访问数据库错误!</Description>
<PosCount></PosCount>
</Result>
<Result>
<ErrorID>10302</ErrorID>
<Description>访问数据库错误!</Description>
<PosCount></PosCount>
</Result>
<Result>
<ErrorID>0</ErrorID>
<Description>发送成功</Description>
<Count>2</Count>
<ErrorCount>0</ErrorCount>
</Result>
</ResultList>"这是一格xml 的字符串,
我想写个读取的方法:
private void ParseXml(string strXml)
{
string errorID="";
string strErrorDetail="";
string strPosCount="";
string strErrorCount="";
string strCount = "";
XmlDocument xd = new XmlDocument();
xd.LoadXml( strXml);
XmlNodeList xnRoot= xd.DocumentElement.SelectNodes ("//Result");
foreach(XmlNode node in xnRoot)
{ XmlNode errorIDNode     = node.SelectSingleNode("//ErrorID");
XmlNode ErrorDetailNode = node.SelectSingleNode("//Description"); XmlNode CountNode       = node.SelectSingleNode("//Count");
XmlNode ErrorCountNode  = node.SelectSingleNode("//errorCount");
XmlNode PosCountNode   = node.SelectSingleNode("//PosCount");
if (errorIDNode!=null)
{
    errorID =errorIDNode.InnerText; 
}
if(ErrorDetailNode!=null)
{
strErrorDetail = ErrorDetailNode.InnerText;
}
if(CountNode!=null)
{
strCount = CountNode.InnerText;
}
if(ErrorCountNode!=null)
{
strErrorCount = ErrorCountNode.InnerText;
}
if(PosCountNode!=null)
{
strPosCount = PosCountNode.InnerText;
}
                         // private string strError  ="";在上面定义的
       this.strError += showError( PosCount,errorID,strErrorDetail);
}
}//showError( PosCount,errorID,strErrorDetail) 这个方法是 显示信息的  
public string  showError(int poscount, string errorCode , string errorDetail)
{
      string displayMessage="";
      if(poscount>0)
      {
displayMessage =  "\r\n" 
+ " >> 错误为第:" + poscount   + "条\r\n"
+ " >> 错误代码:" + errorCode   + "\r\n"
+ " >> 错误信息:" + errorDetail + "\r\n"; return displayMessage;
       }
       else
       {
          displayMessage =  "\r\n" 
+ " >> 错误代码:" + errorCode   + "\r\n"
+ " >> 错误信息:" + errorDetail + "\r\n";
        }
}
为什么 我显示的4 条 记录, 都是 第一条 记录 ,其他的不显示

解决方案 »

  1.   


     >> 错误为第:1条
     >> 错误代码:-1
     >> 错误信息:系统处理单据出错 >> 错误为第:1条
     >> 错误代码:-1
     >> 错误信息:系统处理单据出错 >> 错误为第:1条
     >> 错误代码:-1
     >> 错误信息:系统处理单据出错 >> 错误为第:1条
     >> 错误代码:-1
     >> 错误信息:系统处理单据出错
    结果确实这样 的
      

  2.   

    if(strPosCount.Trim().Length>0)
    {
    PosCount = Convert.ToInt32(strPosCount);
    }
    解析的时候  还有这个 ,忘记贴上了,怕太长了,
    放在 if(PosCountNode!=null)
    {
    strPosCount = PosCountNode.InnerText;
    }的下面了一行
      

  3.   

    你直接用循环读取节点试试,先别用SelectSingleNode()函数。
    每个节点依次的读取。
      

  4.   

    你在循环中每次都选了同一个ID,不要再用selectsingleNode了.
    node已经是在遍历了.