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 竞合关系会是云计算行业主流