原文在百度知道中,来源于多个网友。
新浪微博:http://blog.sina.com.cn/s/blog_ad7fd0f4010180md.html
(一)
保存
var xml =XElement.Load(@"路径");xml.Element("节点名字").AddAfterSelf(new XElement("节点名字","要添加的值"));
xml,Save(@"路径");
读取
var xml =XElement.Load(@"路径");
如果是属性
var query=xml.Element().Where(n=>n.Attribute("比较的节点名字").Value=="名字")
.Select(n=>n.Attribute("要获取的节点名字").Value).Frist();
如果是值
var query=xml.Element().Where(n=>n.Value=="名字")
.Select(n=>n.Value).Frist();
(二)
直接用项目里面的app.config或是web.config最方便。
在里面的appSettings段里加一个元素:
<appSettings>
<add key="mypath" value="thepath"/>
</appSettings>
可以直接用ConfigurationManager读取:
string pathStr = ConfigurationManager.AppSettings["mypath"].ToString();;
(三)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//需要添加的
using System.Xml;
using System.IO;
namespace xml
{
}
=======================
<?xml version="1.0" encoding="utf-8"?>
<config>
</config>
如果config.xml格式正确
会提示
缺少根元素
更改一致就可以了