XML文件格式:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Mobile_area>
    <id>1</id>
    <provincetitle>四川</provincetitle>
    <areatitle>乐山</areatitle>
    <minmobilenum>13419410000</minmobilenum>
    <maxmobilenum>13419419999</maxmobilenum>
  </Mobile_area>
</NewDataSet>这是一个DataTable.WriteXMl 写出来的XML文件。我需要根据minmobilenum 和 maxmobilenum 来判断手机号码是哪个地区的。每秒需要检索50次。,还好考虑CPU使用率的不能高于50%。下面是我现在用的算法,水平有限,热切希望高人指点。
static XmlDataDocument doc = new XmlDataDocument();
do1.Load("TestTable.xml");查询函数:
        public static string SearchFile_1(double caller, string filename)
        {
            XmlElement root =root = doc.DocumentElement;
            foreach (XmlNode child in root.ChildNodes)
            {
                if (child.NodeType == XmlNodeType.Element && child.Name == "Mobile_area")
                {
                    if (caller >= Convert.ToDouble(child.ChildNodes[3].InnerText.ToString()) && caller <= Convert.ToDouble(child.ChildNodes[4].InnerText.ToString()))
                    {
                        return child.ChildNodes[1].InnerText.ToString() + ";" + child.ChildNodes[2].InnerText.ToString();
                    }
                }
            }
            return "";
        }欢迎大家踊跃回帖!谢谢大家不吝赐教!

解决方案 »

  1.   

    提高效率可以考虑使用XmlReader,XmlWriterpublic static string SearchFile_1(double caller, string filename) {
       XmlReader xr=XmlReader.Create("TestTable.xml");
       xr.MoveToElement();
       while(xr.Read()){
         xr.ReadToFollowing("Mobile_area");
         xr.ReadToDescendant("provincetitle");
         string dq=xr.ReadElementContentAsString();
         xr.ReadToNextSibling("areatitle")
         string dq2=xr.ReadElementContentAsString();
         xr.ReadToNextSibling("minmobilenum");
         double d1=xr.ReadElementContentAsDouble();
         if (caller >= d1){
           xr.ReadToNextSibling("maxmobilenum");
           double d2=xr.ReadElementContentAsDouble();
           if(caller <= d2)
              { 
                 return dq+ ";" +dq2;
              } 
           }
       } 

     //参考不一定对,但用XmlReader的效率应该明显高于XmlDocument
      

  2.   

    谢谢大家的回帖。下面是我的测试结果。这里不光次函数,而是一个逻辑执行完的时间。
    用原来的方法,检索一条数据.循环次数50008次,最慢用时:00:00:00.2656250,最快用时:00:00:00.1718750, cpu使用最高80%用ProjectDD朋友的方法:循环次数50008次,最慢用时:00:00:01.0781250,最快用时:00:00:00.8906250,cpu使用最高98%。
    下面是我根据 ProjectDD 朋友的意思整理的函数:
            public static string SearchFile_11(double caller, string filename)
            {
                try
                {   
                    int a=0;
                    if (File.Exists(System.Windows.Forms.Application.StartupPath + "\\"+filename))
                    {
                        using (XmlTextReader xmlReader = new XmlTextReader(System.Windows.Forms.Application.StartupPath + "\\" + filename))
                        {
                            xmlReader.Read();
                            xmlReader.MoveToElement();
                            while (xmlReader.Read())
                            {
                                a++;
                                xmlReader.ReadToFollowing("Mobile_area");                            xmlReader.ReadToDescendant("provincetitle");
                                string province = xmlReader.ReadElementContentAsString();                            xmlReader.ReadToNextSibling("areatitle");
                                string area = xmlReader.ReadElementContentAsString();                            xmlReader.ReadToNextSibling("minmobilenum");
                                double min = xmlReader.ReadElementContentAsDouble();                            xmlReader.ReadToNextSibling("maxmobilenum");
                                double max = xmlReader.ReadElementContentAsDouble();
                                if (caller >= min && caller <= max)
                                {
                                    return province + ";" + area;
                                }
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    throw new Exception(err.ToString());
                }
                return "";
            }
    希望大家踊跃发言!共同学习和进步。