asp.net Linq to Xml學習筆記

來源:互聯網
上載者:User

加上之前學習過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;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.