Linq to XML 增删改查

Linq to XML同样是对原C#访问XML文件的方法的封装,简化了用xpath进行xml的查询以及增加,修改,删除xml元素的操作。

C#访问XML文件的常用类: XmlDocument,XmlElement,XmlAttribute,XmlNode,XmlText等;
Linq to XML 中的常用类 : XDocument,XElement,XAttribute。
废话不多说了,直接上代码:
xml文件数据格式如下
 

public class DataBaseInfo

{

public string ID { getset; }

public string Company { getset; }

public string Server { getset; }

public string DataBase { getset; }

public string UserName { getset; }

public string Password { getset; }

 

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;

}

 

///

/// 

///

///

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

{

//XElementAdd方法

//XElement doc = XElement.Load(filePath);

//doc.Add(db);

 

//XDocumentAdd方法

doc.Element("DataBases").Add(db);

doc.Save(filePath);

return true;

}

catch

{

return false;

}

}

///

/// 

///

///

///

public static bool Remove(string id)

{

XElement xe = (from db in doc.Element("DataBases").Elements("DataBase"wheredb.Attribute("id").Value == id select db).Single() as XElement;

try

{

xe.Remove();

doc.Save(filePath);

return true;

}

catch

{

return false;

 

}

 

}

///

/// 

///

///

public bool Modify()

{

XElement xe = (from db in doc.Element("DataBases").Elements("DataBase"wheredb.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;

}

 

}

///

/// 

///

///

public List<<span style="COLOR: #2b91af">DataBaseInfo> GetAll()

{

 

List<<span style="COLOR: #2b91af">DataBaseInfo> dbs = (from db indoc.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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值