Linq to XML 增删改查

转自: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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值