先在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;
}
}