XML操作集合

 protected override void OnLoad(EventArgs e)
        {
            DataSet dsXML = new DataSet();

            dsXML.ReadXml(Server.MapPath(@"count.xml"));
            int count=0;

            foreach (DataRow dsRow in dsXML.Tables[0].Rows)
            {
                if (dsRow["Number"].ToString() == "1")
                {
                    count = Convert.ToInt32(dsRow["usercount"]);
                }
            }

            count = count + 3;
            Label1.Text = count.ToString();
            //string fileName = "count.xml";
            //dsXML.ReadXml(Server.MapPath(@"count.xml"));
            try
            {
                //XmlDocument doc = new XmlDocument();
                //doc.Load(Server.MapPath(fileName));
                //XmlElement newUser = doc.CreateElement("user");
                //XmlAttribute newID = doc.CreateAttribute("id");
                //newID.InnerText = "3";
                //newUser.SetAttributeNode(newID);
                //XmlElement newUserName = doc.CreateElement("usercount");
                //newUserName.InnerText = count.ToString();
                //newUser.AppendChild(newUserName);
                //doc.DocumentElement.AppendChild(newUser);
                //doc.Save(Server.MapPath(fileName)); 
                foreach (DataRow dsRow in dsXML.Tables[0].Rows)
                {
                    if (dsRow["Number"].ToString() == "1")
                    {
                        dsRow["Number"] = "1";
                        dsRow["usercount"] = count;
                        dsXML.WriteXml(Server.MapPath(@"count.xml"));
                        return;
                    }
                }
            }
            catch (Exception ex)
            {

            }
            finally
            { }

 

 

        }

posted @ 2008-11-21 13:51  paymob  阅读(254)  评论(0编辑  收藏  举报