当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net Linq to Xml学习笔记

asp.net Linq to Xml学习笔记

2017年12月12日  | 移动技术网IT编程  | 我要评论

贺龙的资料,覃洪深,浙江省二本分数线

加上之前学习过linq to entity,因此学习起来也比较随心应手。
以下是项目中某个底层的代码,记下做个备忘,如果能给新手学习linq to xml带来帮助,那就再好不过了
xml文件的格式:
复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<opsystemconfig>
<membercenter>
<domainname>domainname</domainname>
<protocolname>protocolname</protocolname>
<apikey>apikey</apikey>
<aeskey>aeskey</aeskey>
<aesvi>aesvi</aesvi>
</membercenter>
<childsystems>
<childsystem>
<name>content</name>
<controllername>contentmanager</controllername>
</childsystem>
<childsystem>
<name>image</name>
<controllername>imagemanager</controllername>
</childsystem>
<childsystem>
<name>comment</name>
<controllername>commentmanager</controllername>
</childsystem>
<childsystem>
<name>vote</name>
<controllername>votemanager</controllername>
</childsystem>
</childsystems>
</opsystemconfig>
</configuration>

xml增,删,改,查
复制代码 代码如下:

private string docname = string.empty;//配置文件路径
#region isystemmoduleconfigservice 成员
/// <summary>
/// 添加
/// </summary>
/// <param name="name"></param>
/// <param name="controllername"></param>
/// <returns></returns>
public bool add(string name, string controllername)
{
xdocument xdoc = load(docname);
if (isexist(name))
{
xdoc.element("configuration").element("opsystemconfig").element("childsystems").add(new xelement("childsystem",
new xelement("name",name),
new xelement("controllername",controllername)));
xdoc.save(docname);
return true;
}
return false;
}
/// <summary>
/// 修改
/// </summary>
/// <param name="name"></param>
/// <param name="controllername"></param>
/// <returns></returns>
public bool modify(string name, string controllername)
{
xdocument xdoc = load(docname);
if (!isexist(name))
{
var query = from opsystem in xdoc.descendants("childsystem")
where opsystem.element("name").value == name
select opsystem;
foreach (xelement item in query)
{
item.element("controllername").value = controllername;
}
xdoc.save(docname);
return true;
}
return false;
}
/// <summary>
/// 删除
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public bool remove(string name)
{
xdocument xdoc = load(docname);
if (!isexist(name))
{
var query = from opsystem in xdoc.descendants("childsystem")
where opsystem.element("name").value == name
select opsystem;
query.remove();
xdoc.save(docname);
return true;
}
return false;
}
/// <summary>
/// 获得列表
/// </summary>
/// <returns></returns>
public ilist<systemmoduleconfig> getlist()
{
xdocument xdoc = load(docname);
list<systemmoduleconfig> list = new list<systemmoduleconfig>();
var query = from opsystem in xdoc.descendants("childsystem")
select new
{
key = opsystem.element("name").value,
value = opsystem.element("controllername").value
};
foreach (var item in query)
{
systemmoduleconfig config = new systemmoduleconfig();
config.name = item.key;
config.controllername = item.value;
list.add(config);
}
return list;
}
/// <summary>
/// 获得一条childsystem数据
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public systemmoduleconfig getmodel(string name)
{
xdocument xdoc = load(docname);
systemmoduleconfig model = new systemmoduleconfig();
var query = from opsystem in xdoc.descendants("childsystem")
where opsystem.element("name").value == name
select new
{
name = opsystem.element("name").value,
controllername = opsystem.element("controllername").value
};
foreach (var item in query)
{
model.name = item.name;
model.controllername = item.controllername;
}
return model;
}
/// <summary>
/// 加载config文件
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public xdocument load(string path)
{
docname = path;
fileinfo file = new fileinfo(docname);
file.isreadonly = false;
return xdocument.load(docname);
}
/// <summary>
/// 验证name=name的childsystem数据是否存在
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private bool isexist(string name)
{
xdocument xdoc = load(docname);
var query = from opsystem in xdoc.descendants("childsystem")
where opsystem.element("name").value == name
select new
{
name = opsystem.element("name").value
};
if (query.count() == 0)
{
return true;
}
return false;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网