Linq to XML 增删改查
来源:互联网 发布:linux网络编程开源项目 编辑:程序博客网 时间:2024/04/30 06:08
转自:http://de.cel.blog.163.com/blog/static/5145123620119983945167/
public class DataBaseInfo { public string ID { get; set; } public string Company { get; set; } public string Server { get; set; } public string DataBase { get; set; } public string UserName { get; set; } public string Password { get; set; } private static XDocument doc = new XDocument(); public static string filePath = ".\\DataBaseInfo.xml"; public DataBaseInfo() { doc = XDocument.Load(filePath); } public DataBaseInfo(string filepath):this() { filePath = filepath; } /// <summary> /// 增 /// </summary> /// <returns></returns> public bool Add() { XElement db = new XElement("DataBase", new XAttribute("id", ID), new XElement("company", new XAttribute("value",Company)), new XElement("server", new XAttribute("value",Server)), new XElement("database", new XAttribute("value",DataBase)), new XElement("username", new XAttribute("value",UserName)), new XElement("password", new XAttribute("value", Password)) ); try { //用XElement的Add方法 //XElement doc = XElement.Load(filePath); //doc.Add(db); //用XDocument的Add方法 doc.Element("DataBases").Add(db); doc.Save(filePath); return true; } catch { return false; } } /// <summary> /// 删 /// </summary> /// <param name="id"></param> /// <returns></returns> public static bool Remove(string id) { XElement xe = (from db in doc.Element("DataBases").Elements("DataBase") where db.Attribute("id").Value == id select db).Single() as XElement; try { xe.Remove(); doc.Save(filePath); return true; } catch { return false; } } /// <summary> /// 改 /// </summary> /// <returns></returns> public bool Modify() { XElement xe = (from db in doc.Element("DataBases").Elements("DataBase") where db.Attribute("id").Value.ToString() == ID select db).Single(); try { xe.Element("company").Attribute("value").Value = Company; xe.Element("server").Attribute("value").Value = Server; xe.Element("database").Attribute("value").Value = DataBase; xe.Element("username").Attribute("value").Value = UserName; xe.Element("password").Attribute("value").Value = Password; doc.Save(filePath); return true; } catch { return false; } } /// <summary> /// 查 /// </summary> /// <returns></returns> public List<DataBaseInfo> GetAll() { List<DataBaseInfo> dbs = (from db in doc.Element("DataBases").Elements("DataBase") select new DataBaseInfo { ID = db.Attribute("id").Value.ToString(), Company = db.Element("company").Attribute("value").Value.ToString(), Server = db.Element("server").Attribute("value").Value.ToString(), DataBase = db.Element("database").Attribute("value").Value.ToString(), UserName = db.Element("username").Attribute("value").Value.ToString(), Password = db.Element("password").Attribute("value").Value.ToString() }).ToList(); return dbs; }
0 0
- Linq to XML 增删改查
- Linq to Xml 增删查改
- Linq to XML 增删改查
- Linq to XML 增删改查
- Linq to XML增删改查
- Linq to XML 增删改查
- LINQ to XML实现XML文档的增删改查
- Linq To Xml操作XML增删改查
- Linq to xml实现xml数据增删改查
- Linq技术二:Linq to XML及xml增删改查的不同实现方式
- linq for xml增删查改
- ORM, Linq to sql增删改查
- Linq to sql增删改查
- LinQ To Entity的增删改查
- LinQ To Entity的增删改查
- LinQ To Entity的增删改查
- LinQ To Entity的增删改查
- LINQ to SQL之增删改查
- 古典密码学上机实验
- 疯疯癫癫的
- Android中轻松显示Gif图片
- Erlang cowboy 入门参考
- SEO之不能说的秘密
- Linq to XML 增删改查
- CSS魔法堂:盒子模型简介
- Linux系统Loadaverage负载详细解释
- Symfony - view
- Remove Duplicates from Sorted Array II
- 原创内容的五个技巧
- DockerFile详解
- cocos2d-x 3.2 椭圆运动
- 某通信设备集团公司日常巡检应用案例