日期:2014-05-18  浏览次数:20536 次

200分解决一个XML排序问题`麻烦大家了`解决立刻结帖

        protected   void   Page_Load(object   sender,   EventArgs   e)
        {


                StringBuilder   unsorted   =   new   StringBuilder();
                StringBuilder   sorted   =   new   StringBuilder();
                string   xpath   =   "//Title ";
                XPathDocument   doc   =   new   XPathDocument(Server.MapPath( "~/test.xml "));
                XPathNavigator   nav   =   doc.CreateNavigator();
                XPathNodeIterator   nodeIter1   =   nav.Select(xpath);
                while   (nodeIter1.MoveNext())
                {
                        unsorted.Append(nodeIter1.Current.GetAttribute( "publishtime ",   " ")   +   " <br/> ");
                }
                Label1.Text   =   unsorted.ToString();

                XPathExpression   exp   =   nav.Compile( "//Title[@publishtime] ");
                exp.AddSort( "publishtime ",   XmlSortOrder.Descending,   XmlCaseOrder.None,   " ",   XmlDataType.Number);//按时间倒序
                XPathNodeIterator   nodeIter2   =   nav.Select(exp);
                while   (nodeIter2.MoveNext())
                {
                        sorted.Append(nodeIter2.Current.GetAttribute( "publishtime ",   " ")   +   " <br/> ");
                }
                Label2.Text   =   sorted.ToString();   //排序后的所有数据(就是xml文件Title节点的所有属性)需要填充到一个gridview
        }

test.xml
<?xml   version= "1.0 "   encoding= "utf-8 "?>
<TitleCollection>
    <Title   position= "1 "   id= "424 "   title= "aaaadsa "   publishtime= "2007-8-6   0:00:00 "   pic= "~/images/nopig.gif "   informationtype= "测试 "   brief= "dasdas ">
        <contentCollection   content= "dfsfsdfs "   />
    </Title>
    <Title   position= "2 "   id= "425 "   title= "aa "   publishtime= "2007-7-31   0:00:00 "   pic= "~/images/nopig.gif "   informationtype= "新闻 "   brief= "aa ">
        <contentCollection   content= "aaa "   />
  &nbs