先看实例:
在配置文件中加入:
<add key="SaveXmlPath" value="D:\SaveXml.xml"/>
而后创建一个实全类:
public class SingleResultBE { public string Id { get; set; } public string Operation { get; set; } }
private void SavelistToxml(List<SingleResultBE> listInsertResult) { string saveName = ConfigurationManager.AppSettings["SaveXmlPath"]; XElement XmlSaveResult =new XElement("SaveResult", from entity in HavelistResult select new XElement("Result", new XAttribute("ID", string.IsNullOrEmpty(entity.Id)?"1":entity.Id), new XAttribute("Operation", string.IsNullOrEmpty(entity.Operation) ? "1" : entity.Operation) ) ); XmlSaveResult.Save(saveName); }
把一个List<SingleResultBE> 保存到 xml文件中.
读取:
private list<SingleResultBE> ReadxmlForList() { string xmlName = ConfigurationManager.AppSettings["SaveXmlPath"]; XElement docxml = XElement.Load(xmlName); List<SingleResultBE> listHaveInsert = (from entity in docxml.Elements() select entity).ToList(); return listHaveInsert; }
这只是简单用法:
如果要用于实例的话,还要考虑到xmlName这个路径的文件是否存在的问题.