当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

LinqToXML添加XML元素及修改属性值

    public DataSettingWebService()
        {
            this.fileName =this.Server .MapPath(@"/ClientBin/ServerData/UserMessage.xml");
            this.doc = new XmlDocument();
            this.doc.Load(fileName);
        }
 
    添加元素
 
     public  bool  CreateNewUser(string userName,string passWorld)
        {
            try{
                if (userName == "" || passWorld == "")
                {
                    return false;
                }
            XmlElement root = doc.DocumentElement;

            XmlElement newUser = doc.CreateElement("User");

            newUser.SetAttribute("username", userName);
            newUser.SetAttribute("passworld", passWorld);
            newUser.SetAttribute("abilitylevel", "0");
            newUser.SetAttribute("registertime", DateTime.Now.ToString());

            for (int i = 1; i <= 5; i++)
            {
                XmlElement newLevel = doc.CreateElement("Level");
                newLevel.SetAttribute("id", i.ToString());
                newLevel.SetAttribute("score", "0");
                newLevel.SetAttribute("usetime", "0");
                newLevel.SetAttribute("clicknum", "0");
                newLevel.SetAttribute("playtime", "0");
                newUser.AppendChild(newLevel);

            }

            root.AppendChild(newUser);
            doc.Save(fileName);
            return true ;
            }
            catch
            {
                return false ;
            }
        }
 
 
    修改更新属性值

    public bool UpdateUserGrade(string userName, int score, string useTime, string clickNum, int levelId)
        {
        
            bool IsAbilityLevelChange = false;

            XmlNodeList nodelist = doc.GetElementsByTagName("User");

            foreach (XmlNode xdu in nodelist)
            {
                if (xdu.Attributes["username"].Value == userName)
                {

                    XmlElement xeu = xdu as XmlElement;
                    if (levelId > Convert.ToInt32(xeu.Attributes["abilitylevel"].Value))
                    {
                        xeu.SetAttribute("abilitylevel", levelId.ToString());
                        IsAbilityLevelChange = true;
                    }
                    nodelist = xeu.GetElementsByTagName("Level");
                    foreach (XmlNode xdl in nodelist)
                    {
                        if (xdl.Attributes["id"].Value == levelId.ToString())
                        {
                            if (score > Convert.ToInt32(xdl.Attributes["score"].Value))
                            {
                                XmlElement xel = xdl as XmlElement;
                                xel.SetAttribute("score", score.ToString());
                                xel.SetAttribute("usetime", useTime);
                                xel.SetAttribute("clicknum", clickNum);
                                xel.SetAttribute("playtime", DateTime.Now.ToString());
                            }

                            break;
                        }

                    }
                    break;
                }

            }
            doc.Save(fileName);
            return IsAbilityLevelChange;
        }

 

相关内容
赞助商链接