我现在代码是这样:
        XmlDocument doc = new XmlDocument();
        string t1 = "../admin/data/BasketballHistory.xml";
        doc.Load(Server.MapPath(t1));//打开文件
        System.Xml.XmlNodeList nodeList = doc.GetElementsByTagName("ID");
        int id = (Int32.Parse(nodeList[nodeList.Count - 1].InnerText) );
        System.Text.StringBuilder maxID = new System.Text.StringBuilder(id.ToString());
        string sFile = "e:/SlamDunkChinesePortal/sdsite/admin/data" + "/" + maxID + "/"+maxID+".xml";
        doc.Load(sFile);
        int i = int.Parse(doc.DocumentElement.ChildNodes[0].InnerText) + 1;
        doc.DocumentElement.ChildNodes[0].InnerText = i.ToString();
        doc.Save(sFile);
可是为什么不管我刷新哪个,都只有最后那个XML文件的click值+1呢?
int id = (Int32.Parse(nodeList[nodeList.Count - 1].InnerText) );这句代码是取最后一个的ID的值吗?
这个是/admin/data/BasketballHistory.xml的内容:
<?xml version="1.0" encoding="utf-8"?>
<list>
  <theme>
    <title>篮球运动传入中国</title>
    <ID>101</ID>
    <sort>0</sort>
    <date>2004-10-14</date>
  </theme>
  <theme>
    <title>使球从篮筐里落下来竟花了22个春秋</title>
    <ID>102</ID>
    <sort>0</sort>
    <date>2004-10-14</date>
  </theme>
  <theme>
    <title>篮球历史</title>
    <ID>103</ID>
    <sort>0</sort>
    <date>2004-10-14</date>
  </theme>
  <theme>
    <title>你好</title>
    <ID>104</ID>
    <sort>0</sort>
    <date>2004-10-20</date>
  </theme>
  <theme>
    <title>你好</title>
    <ID>105</ID>
    <sort>0</sort>
    <date>2004-10-20</date>
  </theme>
</list>

解决方案 »

  1.   

    回复人: BearRui(孤熊 | 阿弥陀佛!空!) ( ) 信誉:100  2004-10-21 2:08:26  得分: 0  
     
     
       
    System.Xml.XmlNodeList nodeList = doc.SelectNodes("/list/theme/ID");
    int id = (Int32.Parse(nodeList[nodeList.Count - 1].InnerText) );这里得到的ID应该是105  
     
    那我怎么能做到刷新哪个就得到哪个的ID值呢?
      

  2.   

    那我怎么能做到刷新哪个就得到哪个的ID值呢?你的意思是不是得到BasketballHistory.xml文件中的最后一个ID的值!!!
      

  3.   

    BasketballHistory.xml在我的页面里显示的就是5条连接(利用repeater)我希望我点击哪个,就能得到哪个的ID值
      

  4.   

    比如我点击“篮球运动传入中国”,就会得到ID=101,然后在101.xml中的click就自动+1
      

  5.   

    那你点击哪个repeater应该会传递一个什么参数,信息的吧,不然你怎么通过不同的repeater来找ID啊
      

  6.   

    void Page_Load(object sender, EventArgs e)
        {
            string dataFile = Server.MapPath("../admin/data/BasketballHistory.xml");
            System.Data.DataSet ds = new System.Data.DataSet();
            ds.ReadXml(dataFile);        System.Data.DataView pp = new System.Data.DataView(ds.Tables[0]);
            pp.Sort = "ID desc";
            
                    repeater1.DataSource = pp;
            repeater1.DataBind();
        }
      

  7.   

    哦,这个啊,比如你已经知道你点击的title了。string sTitle="篮球运动传入中国";XmlDocument doc=new XmlDocument();
    doc.Load(@"e:\test.xml");XmlNode node=doc.SelectSingleNode("/list/theme[title='"+sTitle+"']");
    string sId=node.ChildNodes[1].InnerText;  // 得到ID//这样得到的结果是101
    //如果你把sTitle改为=“篮球历史”,结果就是103了
      

  8.   

    你有没有QQ啊,我还有大把问题呢:)
    不好意思,我不用QQ也不用MSN的,只上网灌灌水的!!!
      

  9.   

    有点不明白,是不是我要把所有的title都打一遍进去?
      

  10.   

    string sTitle="篮球运动传入中国";
    string sTitle2="";
    string sTitle3="";
    ……
    这样?
      

  11.   

    XmlDocument doc=new XmlDocument();
    doc.Load(@"e:\test.xml");XmlNode node=doc.SelectSingleNode("/list/theme[title='"+sTitle+"']");
    string sId=node.ChildNodes[1].InnerText;  // 得到ID
    ---------------------
    不是啊,你可以把什么代码写成一个函数,参数为sTitle,返回值为sId,当你点击了你的repeater后,看你要得到的节点是“篮球运动传入中国”还是其他什么,把这个字符串做为参数调用你写成的函数,然后不就直接返回了sId给了啦。
      

  12.   

    private string GetID(string sTitle)
    {
    XmlDocument doc=new XmlDocument();
    doc.Load(@"e:\test.xml");XmlNode node=doc.SelectSingleNode("/list/theme[title='"+sTitle+"']");
    string sId=node.ChildNodes[1].InnerText;return sId;
    }然后当你点击了"篮球历史"这个repeater后,你可以直接:string id=GetID("篮球历史");
    这个id就是103了!!!
      

  13.   

    对了,顺便问一下,如果象我这样的拿XML存储数据,要做全文搜索的话困难吗?比如搜索“按标题”就对应搜索XML里的title节点。按作者就对应另一个。按关键字,就对应所有节点,可以吗?
      

  14.   

    好,回家试试,谢谢!哈,米兰赢了吧哈哈,我对足球不懂的,我随便猜的。
    ------------------------------------------
    如果象我这样的拿XML存储数据,要做全文搜索的话困难吗?比如搜索“按标题”就对应搜索XML里的title节点。按作者就对应另一个。按关键字,就对应所有节点,可以吗?利用XPath可以轻松实现查找,给可你学XPATH的地方。
    http://www.w3schools.com/xpath/default.asp
      

  15.   

    这么写对吗?    private string GetID(string sTitle)
        {
            XmlDocument doc1 = new XmlDocument();
            string t1 = "../admin/data/BasketballHistory.xml";
            doc1.Load(Server.MapPath(t1));//打开文件
            System.Xml.XmlNodeList nodeList = doc1.GetElementsByTagName("ID");
            int id = (Int32.Parse(nodeList[nodeList.Count - 1].InnerText));
            System.Text.StringBuilder maxID = new System.Text.StringBuilder(id.ToString());
            string sFile = "e:/SlamDunkChinesePortal/sdsite/admin/data" + "/" + maxID + "/" + maxID + ".xml";
            doc1.Load(sFile);        XmlNode node = doc1.SelectSingleNode("/list/theme/[title='" + sTitle + "']");
            string sId = node.ChildNodes[1].InnerText;        return sId;
        }然后在page_load里写
    XmlDocument doc = new XmlDocument();
            string id = GetID();        string sFile = "e:/SlamDunkChinesePortal/sdsite/admin/data" + "/" + id + "/" + id + ".xml";
            doc.Load(sFile);
            int i = int.Parse(doc.DocumentElement.ChildNodes[0].InnerText) + 1;
            doc.DocumentElement.ChildNodes[0].InnerText = i.ToString();
            doc.Save(sFile);
      

  16.   

    你那函数不是得到ID的值吗?可我接着还要把这个ID对应的XML中的CLICK节点+1啊……
      

  17.   

    先用我的函数得到sid;然后:
    string sFile = "e:/SlamDunkChinesePortal/sdsite/admin/data" + "/"+ sid + ".xml";XmlDocument doc=new XmlDocument();
    doc.Load(sFile);XmlNode node=doc.SelectSingleNode("//click");//选择click节点,click是区分大小写的。
    int i=int.Parse(node.InnerText)+1;node.InnerText=i.ToString();doc.Save(sFile);
      

  18.   

    是这样吗……
    void Page_Load(object sender, EventArgs e)
    {private string GetID(string sTitle)
    {
    XmlDocument doc=new XmlDocument();
    doc.Load(@"e:\test.xml");XmlNode node=doc.SelectSingleNode("/list/theme[title='"+sTitle+"']");
    string sId=node.ChildNodes[1].InnerText;return sId;
    }
    string sFile = "e:/SlamDunkChinesePortal/sdsite/admin/data" + "/"+ sid + ".xml";XmlDocument doc=new XmlDocument();
    doc.Load(sFile);XmlNode node=doc.SelectSingleNode("//click");//选择click节点,click是区分大小写的。
    int i=int.Parse(node.InnerText)+1;node.InnerText=i.ToString();doc.Save(sFile);}
      

  19.   

    public partial class History_aspx
    {
        System.Data.DataSet ds = new System.Data.DataSet();
        private string GetID(string sTitle)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"e:\test.xml");        XmlNode node = doc.SelectSingleNode("/list/theme[title='" + sTitle + "']");
            string sId = node.ChildNodes[1].InnerText;        return sId;
        }
        void Page_Load(object sender, EventArgs e)
        {        string sFile = "e:/SlamDunkChinesePortal/sdsite/admin/data" + "/" + sId +"/"+sId+ ".xml";        XmlDocument doc = new XmlDocument();
            doc.Load(sFile);        XmlNode node = doc.SelectSingleNode("//click");//选择click节点,click是区分大小写的。
            int i = int.Parse(node.InnerText) + 1;        node.InnerText = i.ToString();        doc.Save(sFile);
    }显示错误:Compiler Error Message: CS0103: The name 'sId' does not exist in the current context
      

  20.   

    string sFile = "e:/SlamDunkChinesePortal/sdsite/admin/data" + "/" + sId +"/"+sId+ ".xml";前面加一句:sId=GetID("篮球历史");楼主看样子是刚学编程不久吧!!!
      

  21.   

    是的,我就是最近才边看一个论坛源代码边开始学的。
    我刚把代码改成这样了,你看看:
        void Page_Load(object sender, EventArgs e)
        {
            string id = Request.QueryString["type"];
            string sFile = "../admin/data" + "/" + id +"/"+id+ ".xml";        XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(sFile));        XmlNode node = doc.SelectSingleNode("//click");//选择click节点,click是区分大小写的。
            int i = int.Parse(node.InnerText) + 1;        node.InnerText = i.ToString();        doc.Save(sFile);
    }
    你觉得可以吗?因为我觉得用E:是不是会以后到别的机器上出现问题。
    可是运行了还是有这样的错误:
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
      

  22.   

    哈哈哈哈,终于成功了!!!!!!!!!!!!!!!!doc.Save(sFile);写成doc.Save(Server.MapPath(sFile));就对了,谢谢兄弟的耐心啊!呵呵!
      

  23.   

    再问再问,能不能达到这个功能。当每天的凌晨0点,click值清0,重新开始统计。行吗?也就是说只统计当天的访问数值。或者当每周一的凌晨0点,click清0,统计的是周访问量,该怎么实现呀。
      

  24.   

    这个每做过的,不是很清楚,你可以找找那些论坛的代码,一般论坛的代码有这样的例子。
    ---------------------------------按我的思维,在你的XML文件中多放个节点来保存XML文件最后访问的时间,然后每次访问XML的时候,就把最后访问的时间和现在的时间比较,如果发现时间是新的一天,就把click清0再+1;这样虽然不是每天的凌晨0点来清0,但是也保证是有第一个访问者的时候清0的。只是个人的想法,行不行就不知道了!!!
      

  25.   

    现在这样一个XML
    <?xml version="1.0" encoding="utf-8"?>
    <list>
      <theme>
        <title>篮球运动传入中国</title>
        <ID>101</ID>
        <sort>0</sort>
        <date>2004-10-14</date>
        <click>0<click>
      </theme>
      <theme>
        <title>使球从篮筐里落下来竟花了22个春秋</title>
        <ID>102</ID>
        <sort>0</sort>
        <date>2004-10-14</date>
        <click>0<click>
      </theme>
      <theme>
        <title>篮球历史</title>
        <ID>103</ID>
        <sort>0</sort>
        <date>2004-10-14</date>
        <click>0<click>
      </theme>
    </list>
    我想在repeater中点击其中任意一个它的click就+1,这是我写的代码:
            string id = Request.QueryString["type"];
            string File = "../admin/data/Basketball.xml";
            XmlDocument doc1 = new XmlDocument();
            doc1.Load(Server.MapPath(File));
            XmlNode node1 = doc.SelectSingleNode("//click[ID='id']");
            int i1 = int.Parse(node1.InnerText) + 1;        node1.InnerText = i1.ToString();        doc1.Save(Server.MapPath(File));
    哪里错了啊?显示不了