XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(@"c:\test.xml");
XmlNodeList nodelist = xmldoc.GetElementsByTagName["person"];
for(int i=0;i<nodelist.Length-1;i++)
{
   if (Convert.ToInt32(nodelist[i].InnerText) >=2 || Convert.ToInt32(nodelist[i].InnerText) <=4)
         ........................
}

解决方案 »

  1.   

    your xml is invalid, change the last <data> to </data>, tryusing System;
    using System.Xml;static void GetXml(XmlDocument doc, int start, int end)
    {
        foreach (XmlNode node in doc.SelectNodes(String.Format("data/person[. < {0} or . > {1}]", start, end)))
    doc.DocumentElement.RemoveChild(node);}static void Test5()
    {
     
     XmlDocument xmldoc = new XmlDocument(); xmldoc.Load("data.xml"); GetXml(xmldoc, 2,4); Console.WriteLine(xmldoc.InnerXml);
    }
      

  2.   

    谢谢两位。
    可能我有些菜。
    foreach (XmlNode node in doc.SelectNodes(String.Format("data/person[. < {0} or . > {1}]", start, end)))
    上面一行我还是有点不明白,我去查查。
    我把函数再定义一下。希望大家帮帮忙。XmlDocument GetXmlDoc( XmlDocument xmldocAll, int nStart, int nEnd);
      

  3.   

    [. < {0} or . > {1}] 呵呵竟让我看糊涂了。我分出来看,哎就清晰了。
    谢谢。我看懂了。就揭贴。
      

  4.   

    basically, it uses an XPath to select all nodes whose person's value is < start or > end, then remove those nodesString.Format("data/person[. < {0} or . > {1}]", 2, 4)
    ==>
    "data/person[. < 2 or . > 4]"//the following method has a side effect
    XmlDocument GetXmlDoc( XmlDocument xmldocAll, int nStart, int nEnd)
    {
      foreach (XmlNode node in  xmldocAll.SelectNodes(String.Format("data/person[. < {0} or . > {1}]", start, end)))
    xmldocAll.DocumentElement.RemoveChild(node);  return xmldocAll;}
    if you don't want xmldocAll to be changed, clone one firstXmlDocument GetXmlDoc( XmlDocument xmldocAll, int nStart, int nEnd)
    {
      XmlDocument doc = (XmlDocument)xmldocAll.CloneNode(true);
      foreach (XmlNode node in doc.SelectNodes(String.Format("data/person[. < {0} or . > {1}]", start, end)))
    doc.DocumentElement.RemoveChild(node);  return doc;}
      

  5.   

    请教wangj2001(乡村酒吧) ,正则表达式如何实现XPATH的功能?