首页 > 编程 > .NET > 正文

asp.net Linq to Xml学习笔记

2024-07-10 12:42:30
字体:
来源:转载
供稿:网友
加上之前学习过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")
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表