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漏洞攻击植入木马逆向分析