加上之前学习过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;
}
标签:
Linq,Xml

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com

评论“asp.net Linq to Xml学习笔记”

暂无“asp.net Linq to Xml学习笔记”评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。