asp.net Linq to Xml學(xué)習(xí)筆記
更新時間:2010年03月11日 22:04:23 作者:
之前都沒有學(xué)習(xí)過關(guān)于XML文件的操作,由于最近開發(fā)的項目需要用到,開始時學(xué)習(xí)了原始的XML文件操作方法,看了半天,也看的頭暈眼花,沒學(xué)習(xí)到真正的用法,后來在同事的推薦下學(xué)習(xí)了Linq to Xml
加上之前學(xué)習(xí)過Linq to Entity,因此學(xué)習(xí)起來也比較隨心應(yīng)手。
以下是項目中某個底層的代碼,記下做個備忘,如果能給新手學(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>
/// 驗證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;
}
以下是項目中某個底層的代碼,記下做個備忘,如果能給新手學(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>
/// 驗證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;
}
您可能感興趣的文章:
相關(guān)文章
動態(tài)加載Js代碼到Head標(biāo)簽中的腳本
我遇到了這樣的問題,請教google,結(jié)果大多數(shù)只是介紹那個注冊js的幾個函數(shù),而這幾個函數(shù)插入的js都在body里面,幸而在老外那里看到了這個代碼,其實比較簡單,但夠有用2009-01-01
Asp.Net?Core配置多環(huán)境log4net配置文件的全過程
在.NET世界中有非常多的日志框架,然而log4net是目前為止最流行的一款日志框架,下面這篇文章主要給大家介紹了關(guān)于Asp.Net?Core配置多環(huán)境log4net配置文件的相關(guān)資料,需要的朋友可以參考下2022-04-04
asp.net 驗證字符串是否為純數(shù)字檢測函數(shù)
如何驗證字符串是否為純數(shù)字2010-03-03
asp.net 頁面版文本框智能提示JSCode (升級版)
模擬百度,Google智能提示,非與服務(wù)器端交互的,數(shù)據(jù)源來自已經(jīng)綁定好的下拉列表。純客戶端腳本 升級版2009-12-12
ASP.NET AJAX 4.0的模版編程(Template Programming)介紹
不過當(dāng)我評估ASP.NET AJAX 4.0的時候,我確實被它的特征給震住了。新的特征完全專注于瀏覽器技術(shù),比如XHTML和javascript。 我非常欽佩ASP.NET AJAX小組。2009-07-07
Asp.net FileUpload+Image制作頭像效果示例代碼
個人信息中通常需要自己的頭像或者照片,今天主要介紹一下使用FileUpload+img控件上傳照片,感興趣的朋友可以參考下2013-08-08
JavaScript用JQuery呼叫Server端方法實現(xiàn)代碼與參考語法
從Javascript客戶端用JQuery呼叫Server端的方法,這也是一個大膽的嘗試,本人做了演示動畫以及參考語法,感興趣的朋友可以參考下,希望本人對你有所幫助2013-01-01
asp.net gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁
這篇文章主要介紹了asp.net gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁,可使用上下鍵選中行,選中后點擊修改,textbox獲得gridview中的代碼的數(shù)據(jù),需要的朋友可以參考下2014-12-12

