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-03 20:19:49
上一篇:
Spring中的注解
下一篇:
Android開發12——Andorid中操作數據庫的insert的兩種方法以及nullColumnHack
采訪阿裏服務器專家鍾楊帆,看阿裏全浸沒式液冷集群有哪些黑科技
RHEL安裝heartbeat報錯: heartbeat: udpport setting must precede media statements
遠程辦公工資高嗎?你快樂嗎?看網友機智回答
cocos2d 在windows環境下搭建
Android開發10——Activity的跳轉與傳值_顯示意圖(intent)的應用
來源於WCF的設計模式:可擴展對象模式[上篇]
傳統IT企業轉型之路
【Java】ArrayList 的 toArray() 方法拋出 ClassCastException 異常
雲市場合作夥伴-袋鼠雲獲A輪融資,成立一年半獲三輪投資超億元
Redis漏洞攻擊植入木馬逆向分析