asp.net Linq to Xml學(xué)習(xí)筆記
更新時(shí)間:2010年03月11日 22:04:23 作者:
之前都沒有學(xué)習(xí)過關(guān)于XML文件的操作,由于最近開發(fā)的項(xiàng)目需要用到,開始時(shí)學(xué)習(xí)了原始的XML文件操作方法,看了半天,也看的頭暈眼花,沒學(xué)習(xí)到真正的用法,后來在同事的推薦下學(xué)習(xí)了Linq to Xml
加上之前學(xué)習(xí)過Linq to Entity,因此學(xué)習(xí)起來也比較隨心應(yīng)手。
以下是項(xiàng)目中某個(gè)底層的代碼,記下做個(gè)備忘,如果能給新手學(xué)習(xí)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數(shù)據(jù)
/// </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>
/// 驗(yàn)證Name=name的ChildSystem數(shù)據(jù)是否存在
/// </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;
}
以下是項(xiàng)目中某個(gè)底層的代碼,記下做個(gè)備忘,如果能給新手學(xué)習(xí)Linq to Xml帶來幫助,那就再好不過了
XML文件的格式:
復(fù)制代碼 代碼如下:
<?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增,刪,改,查
復(fù)制代碼 代碼如下:
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數(shù)據(jù)
/// </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>
/// 驗(yàn)證Name=name的ChildSystem數(shù)據(jù)是否存在
/// </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;
}
您可能感興趣的文章:
- asp.net使用LINQ to SQL連接數(shù)據(jù)庫及SQL操作語句用法分析
- asp.net中一個(gè)linq分頁實(shí)現(xiàn)代碼
- asp.net中通過ALinq讓Mysql操作變得如此簡單
- asp.net 根據(jù)漢字的拼音首字母搜索數(shù)據(jù)庫(附 LINQ 調(diào)用方法)
- asp.net LINQ中數(shù)據(jù)庫連接字符串的問題
- asp.net Linq TO Sql 分頁方法
- asp.net Linq To Xml上手Descendants、Elements遍歷節(jié)點(diǎn)
- .NET 9 中 LINQ 新增功能實(shí)現(xiàn)過程
相關(guān)文章
Asp.Net 程序錯(cuò)誤Runtime Error原因與解決
提示這個(gè),不管怎么改配置文件的設(shè)置都不行,下面是修正方法,大家可以試試。2010-03-03.net實(shí)現(xiàn)網(wǎng)站用戶登錄認(rèn)證
本文給大家介紹的是.net實(shí)現(xiàn)網(wǎng)站用戶登錄認(rèn)證的方法和實(shí)例,都非常的簡單實(shí)用,需要的小伙伴可以參考下。2015-11-11this connector is disabled錯(cuò)誤的解決方法
打開editor/filemanager/connectors/aspx/config.ascx修改CheckAuthentication()方法,返回true2008-11-11Asp.Net?Core配置多環(huán)境log4net配置文件的全過程
在.NET世界中有非常多的日志框架,然而log4net是目前為止最流行的一款日志框架,下面這篇文章主要給大家介紹了關(guān)于Asp.Net?Core配置多環(huán)境log4net配置文件的相關(guān)資料,需要的朋友可以參考下2022-04-04asp.net Menu控件+SQLServer實(shí)現(xiàn)動(dòng)態(tài)多級菜單
asp.net Menu控件+SQLServer實(shí)現(xiàn)動(dòng)態(tài)多級菜單的代碼,需要的朋友可以參考下。2011-12-12ASP.NET Core 文件響應(yīng)壓縮的常見使用誤區(qū)
在微軟官方文檔中,未明確指出文件壓縮功能的使用誤區(qū)。本文將對 ASP.NET Core 文件響應(yīng)壓縮的常見使用誤區(qū)做出說明。2021-05-05使用HttpWebRequest向網(wǎng)站模擬上傳數(shù)據(jù)
使用HttpWebRequest向網(wǎng)站模擬上傳數(shù)據(jù)...2006-09-09簡單幾步 實(shí)現(xiàn)vs2010對html5的支持
微軟從來不會(huì)讓程序員用記事本寫代碼,如今html5馬上就要火起來vs2010怎么會(huì)不支持html5呢?月月bird我將vs2010支持html5的方法整理了一下。2016-05-05