private void setxmlvalue(string path,string key,string value )
{
XElement xe = XElement.Load(path );
IEnumerable<XElement> element = from ele in xe.Elements("param")
where ele.Attribute("key").Value == key
select ele;
if (element.Count() > 0)
{
XElement first = element.First();
///设置新的属性
first.SetValue(value);
}
xe.Save(path);
}
xml 文件格式
<?xml version="1.0" encoding="utf-8"?>
<config>
<param key="nginxIP">127.0.0.1</param>
<param key="nginxPort">9090</param>
</config>
private void setcontectvalue(string path, string key, string value)
{
XElement xe = XElement.Load(path);
IEnumerable<XElement> element = from ele in xe.Elements("Resource")
select ele;
element.First().SetAttributeValue(key, value);
xe.Save(path);
}
<?xml version="1.0" encoding="utf-8"?>
<Context swallowOutput="true">
<WatchedResource>WEB</WatchedResource>
<Resource name="videoweb" type="javax.sql.DataSource" password="root" driverClassName="com.mysql.jdbc.Driver" maxIdle="20" maxWait="500" username="root" url="127.0.0.1:8080" maxActive="50" />
</Context>