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
            { }

 

 

        }

原文地址:https://www.cnblogs.com/duwamish/p/1338400.html