我想读写的xml文件内容如下:
<?xml version="1.0"encoding="utf-8"?>
<configuration>
<appSettings>
<add key="IP" value="X.X.X.X"/>
<add key="Port" value="X"/>
<add key="Name" value="XX"/>
</appSettings>
</configuration>问题是:如何对"X"部分进行读写,望高手指教.
<?xml version="1.0"encoding="utf-8"?>
<configuration>
<appSettings>
<add key="IP" value="X.X.X.X"/>
<add key="Port" value="X"/>
<add key="Name" value="XX"/>
</appSettings>
</configuration>问题是:如何对"X"部分进行读写,望高手指教.
System.Xml.Xpath去看看MSDN中这两个命名空间的内容
System.Configuration.ConfigurationSettings.AppSettings["Data"]xml文档
XmlDocument xDoc = new XmlDocument();
xDoc.Load(Application.ExecutablePath + ".config"); XmlNode xNode;
XmlElement xElem1;
XmlElement xElem2; xNode = xDoc.SelectSingleNode("//appSettings"); xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", AppKey);
xElem2.SetAttribute("value", AppValue);
xNode.AppendChild(xElem2);
}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
参数
name
要创建或更改的属性的名称。这是限定名。如果该名称包含一个冒号,则将其解析为前缀和本地名称两个部分。 value
要为此属性设置的值。 eg.
using System;
using System.IO;
using System.Xml;public class Sample
{
public static void Main()
{ XmlDocument doc = new XmlDocument();
doc.LoadXml("<book xmlns:bk='urn:samples' bk:ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>"); XmlElement root = doc.DocumentElement; // Add a new attribute.
root.SetAttribute("genre", "novel"); Console.WriteLine("Display the modified XML...");
Console.WriteLine(doc.InnerXml); }
}
//读取
string port = config.AppSettings.Settings["Port"].Value;
Console.WriteLine(port);
//更改
config.AppSettings.Settings["Port"].Value = "test";
config.Save();
port = config.AppSettings.Settings["Port"].Value;
Console.WriteLine(port);
X
testdon't forget using System.Configuration;
string Port=appSettings["Port"];
string Name=appSettings["Name"];
在编译时下面这段提示找不到相关类或
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
是不是还有什么命名空间没有包括在里面?
using System.Configuration;
如果还不行把System.Configuration.Install这个也加上