<?xml version='1.0' encoding='UTF-8'?>
<data albumText='2advanced' gap='-5' scrollSpeed='5'>
<pic linkURL='http://www.gofordesign.com/' imgURL='flash/images/01.jpg' />
<pic linkURL='http://www.gofordesign.com/' imgURL='flash/images/02.jpg' />
<pic linkURL='http://www.gofordesign.com/' imgURL='flash/images/03.jpg' />
<pic linkURL='http://www.gofordesign.com/' imgURL='flash/images/04.jpg' />
<pic linkURL='http://www.gofordesign.com/' imgURL='flash/images/05.jpg' />
</data>protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(Server.MapPath("~/aa/ad.xml"));
        XmlNodeList nodelist = xml.SelectSingleNode("data").ChildNodes;
        foreach (XmlNode xn in nodelist)
        {              
            for (int i = 1; i <= nodelist.Count; i++)
            {
                DataSet ds = new DataSet();
                ds = Class1.asp();
                XmlElement xe = (XmlElement)xn;
                string s = ds.Tables[0].Rows[i-1]["linkURL"].ToString();
                string t = ds.Tables[0].Rows[i-1]["imgURL"].ToString();
                xe.SetAttribute("linkURL",s );
                xe.SetAttribute("imgURL",t );
                
            }
        }
        xml.Save(Server.MapPath("~/aa/ad.xml"));
    }
从数据库中读出5条数据,想一条条修改xml中的值的,现在把xml中的值都该为数据库中最后一条记录了

解决方案 »

  1.   

    弱弱的问下,foreach里嵌套for循环是做什么的
    你现在这样,每取一个XmlNode都循环一遍,最后当然都改为数据库中最后一条记录了这样试下    protected void Button1_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds = Class1.asp();        XmlDocument xml = new XmlDocument();
            xml.Load(Server.MapPath("~/aa/ad.xml"));
            XmlNodeList nodelist = xml.SelectSingleNode("data").ChildNodes;
            for (int i = 0; i < nodelist.Count;i++)
            {              
                XmlElement xe = (XmlElement)xn;
                string s = ds.Tables[0].Rows[i]["linkURL"].ToString();
                string t = ds.Tables[0].Rows[i]["imgURL"].ToString();
                xe.SetAttribute("linkURL",s );
                xe.SetAttribute("imgURL",t );        
            }
            xml.Save(Server.MapPath("~/aa/ad.xml"));
        }ds.Tables[0].Rows[i]下标是否越界最好是先做下判断
      

  2.   

    foreach遍历了在for循环就没必要了