web前端

位置:IT落伍者 >> web前端 >> 浏览文章

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


发布日期:2019年07月10日
 
LinqToXML添加XML元素及修改属性值

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;

}

               

上一篇:奇怪的html控件textarea

下一篇:GhostDoc 自动生成 XML 注释