欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net Linq to Xml学习笔记

程序员文章站 2024-03-07 23:08:27
加上之前学习过linq to entity,因此学习起来也比较随心应手。 以下是项目中某个底层的代码,记下做个备忘,如果能给新手学习linq to xml带来帮助,那就再好...
加上之前学习过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;
}