linq to xml之增改刪查
有如下xml文檔
<?xml version="1.0" encoding="utf-8"?>
<users>
<user>
<id>001</id>
<name>xy</name>
<sex>male</sex>
<age>30</age>
<university>清華</university>
<phone>13328981122</phone>
</user>
<user>
<id>002</id>
<name>小明</name>
<sex>male</sex>
<age>19</age>
<university>北大</university>
<phone>13328980192</phone>
</user>
<user>
<id>003</id>
<name>小張</name>
<sex>female</sex>
<age>30</age>
<university>南大</university>
<phone>13328981152</phone>
</user>
</users>
對其進行操作
class XMLService : IXMLService
{
// xml文檔路徑
string path = Environment.CurrentDirectory + "\\xml\\communication.xml";
XDocument doc = null;
public void CreateXML()
{
// 創建xml元素
XElement root = new XElement("users",
new XElement("user",
new XElement("id", "001"),
new XElement("name", "xy"),
new XElement("sex", "male"),
new XElement("age", "30"),
new XElement("university", "清華"),
new XElement("phone", "13328981122")
),
new XElement("user",
new XElement("id", "002"),
new XElement("name", "小明"),
new XElement("sex", "male"),
new XElement("age", "19"),
new XElement("university", "北大"),
new XElement("phone", "13328980192")
),
new XElement("user",
new XElement("id", "003"),
new XElement("name", "小張"),
new XElement("sex", "female"),
new XElement("age", "30"),
new XElement("university", "南大"),
new XElement("phone", "13328981152")
)
);
root.Save(path);
}
// 讀出所有通訊錄
public IEnumerable<Information> ReadAllInformation()
{
var users = from c in XElement.Load(path).Elements("user")
select c;
return ToInforList(users);
}
// 根據姓名查找
public IEnumerable<Information> FindInfoByName(string name)
{
var users = from c in XElement.Load(path).Elements("user")
where c.Element("name").Value.Contains(name.Trim())
select c;
return ToInforList(users);
}
// 轉化成List<Information>
public IEnumerable<Information> ToInforList(IEnumerable<XElement> users)
{
List<Information> listInfo = new List<Information>();
foreach (var user in users)
{
Information info = new Information();
info.Id = user.Element("id").Value.ToString();
info.Name = user.Element("name").Value.ToString();
info.Gender = user.Element("sex").Value.ToString();
int age;
if (int.TryParse(user.Element("age").Value.ToString(), out age))
{
info.Age = age;
}
info.University = user.Element("university").Value.ToString();
info.Phone = user.Element("phone").Value.ToString();
listInfo.Add(info);
}
return listInfo;
}
// 刪除
public void DeleteByID(string id)
{
doc = XDocument.Load(path);
doc.Element("users").Elements("user").
Where(ss => ss.Element("id").Value == id).Remove();
doc.Save(path);
}
// 增加一條記錄
public void Add(string id, string name, string sex, int age, string school, string phone)
{
doc = XDocument.Load(path);
doc.Element("users").Add(new XElement("user",
new XElement("id", id),
new XElement("name", name),
new XElement("sex", sex),
new XElement("age", age.ToString()),
new XElement("university",
school),
new XElement("phone",
phone)));
doc.Save(path);
}
// 修改
public void Modify
(string id,string name, string sex, int age, string school, string phone)
{
doc = XDocument.Load(path);
var user = (from u in doc.Element("users").Elements("user")
where u.Element("id").Value == id
select u).FirstOrDefault();
user.Element("name").Value = name;
user.Element("sex").Value = sex;
user.Element("age").Value = age.ToString();
user.Element("university").Value = school;
user.Element("phone").Value = phone;
doc.Save(path);
}
最後更新:2017-04-02 06:52:25
上一篇:
OPhone程序開發入門之音樂播放器
下一篇:
FileUpload過濾文件類型
滿足數字業務和雲計算需求的新的廣域網架構
struts中采用注解配置Action
思科HyperFlex在超融合市場掀起熱潮
nginx站點配置之域名301重定向
理解 Objective-C 的 ARC
華為董事長孫亞芳登上 《福布斯》全球女強人榜
Android應用安裝錯誤:INSTALL_FAILED_MEDIA_UNAVAILABLE
Android中安裝軟件到模擬器時提示:INSTALL_FAILED_INSUFFICIENT_STORAGE 解決辦法
輕輕幾個點擊,在 AWS 和 Azure 上搭建 Docker 數據中心
阿裏雲發布新一代處理器矛頭直指AWS 競合關係會是雲計算行業主流