熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2022-06-13   來源: .NET編程 

  public DataSettingWebService()
        {
            thisfileName =thisServer MapPath(@/ClientBin/ServerData/UserMessagexml);
            thisdoc = new XmlDocument();
            thisdocLoad(fileName);
        }
 
    添加元素
 
     public  bool  CreateNewUser(string userNamestring passWorld)
        {
            try{
                if (userName == || passWorld == )
                {
                    return false;
                }
            XmlElement root = docDocumentElement;

  XmlElement newUser = docCreateElement(User);

  newUserSetAttribute(username userName);
            newUserSetAttribute(passworld passWorld);
            newUserSetAttribute(abilitylevel );
            newUserSetAttribute(registertime DateTimeNowToString());

  for (int i = ; i <= ; i++)
            {
                XmlElement newLevel = docCreateElement(Level);
                newLevelSetAttribute(id iToString());
                newLevelSetAttribute(score );
                newLevelSetAttribute(usetime );
                newLevelSetAttribute(clicknum );
                newLevelSetAttribute(playtime );
                newUserAppendChild(newLevel);

  }

  rootAppendChild(newUser);
            docSave(fileName);
            return true ;
            }
            catch
            {
                return false ;
            }
        }
 
 
    修改更新屬性值

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

  XmlNodeList nodelist = docGetElementsByTagName(User);

  foreach (XmlNode xdu in nodelist)
            {
                if (xduAttributes[username]Value == userName)
                {

  XmlElement xeu = xdu as XmlElement;
                    if (levelId > ConvertToInt(xeuAttributes[abilitylevel]Value))
                    {
                        xeuSetAttribute(abilitylevel levelIdToString());
                        IsAbilityLevelChange = true;
                    }
                    nodelist = xeuGetElementsByTagName(Level);
                    foreach (XmlNode xdl in nodelist)
                    {
                        if (xdlAttributes[id]Value == levelIdToString())
                        {
                            if (score > ConvertToInt(xdlAttributes[score]Value))
                            {
                                XmlElement xel = xdl as XmlElement;
                                xelSetAttribute(score scoreToString());
                                xelSetAttribute(usetime useTime);
                                xelSetAttribute(clicknum clickNum);
                                xelSetAttribute(playtime DateTimeNowToString());
                            }

  break;
                        }

  }
                    break;
                }

  }
            docSave(fileName);
            return IsAbilityLevelChange;
        }


From:http://tw.wingwit.com/Article/program/net/201311/12271.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.