读取xml文档并排序

先在c:/temp 下建立xml文件 XMLFile1.xml

<?xml version="1.0" encoding="utf-8"?>
<root>

  <city>
    <region>
      <score>3</score>
    </region>
    <region>
      <score>2</score>
    </region>
    <region>
      <score>5</score>
    </region>
    <region>
      <score>7</score>
    </region>
  </city>
</root>

 

程序:

class Program
    {
        static void Main(string[] args)
        {
            XMLParser parser = new XMLParser();
            parser.Do();

        }
    }

    class XMLParser
    {

        public void Do()
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(@"c:/temp/XMLFile1.xml");

            XmlNodeList list= xdoc.SelectNodes(@"/root/city/region/score");
          
             int [] nodes=new int[list.Count];

             for (int i = 0; i < nodes.Length; i++)
             {
                 nodes[i] =Convert.ToInt32( list[i].FirstChild.Value);
             }

             QuickSort sorter = new QuickSort();
             sorter.Sort(nodes, 0, nodes.Length - 1);

             for (int i = 0; i < nodes.Length; i++)
             {

                 Console.WriteLine(nodes[i]);
             }
             Console.Read();

 


        }


    }

  //排序算法

    class QuickSort
    {

        public void Sort(int[] K, int s, int t)
        {
            if (s < t)
            {
                int i = s;
                int j = t + 1;
                int temp = K[s];


                while (true)
                {
                    do
                    {
                        i++;

                    }
                    while (temp > K[i] && i != t);

                    do
                    {

                        j--;
                    }
                    while (temp < K[j] && j != s);

 

                    if (i < j)
                    {
                        Swap(ref K[i], ref  K[j]);
                    }
                    else
                    {
                        goto LabelOut;
                    }

                }

 

            LabelOut: Swap(ref K[s], ref K[j]);


                Sort(K, s, j - 1);//j 的左边总小于等于K[j],j的右边总大于等于K[j];
                Sort(K, j + 1, t);

 


            }
        }

        public void Swap(ref  int a, ref  int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }
    }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值