我想读写的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"部分进行读写,望高手指教.

解决方案 »

  1.   

    读写属性System.Xml
    System.Xml.Xpath去看看MSDN中这两个命名空间的内容
      

  2.   

    config文件的话 直接
    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");
      

  3.   

    XmlElement.SetAttribute 方法 (String, String) 
    参数
    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);  }
    }
      

  4.   

    try..Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                //读取
                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);
      

  5.   

    输出:
    X
    testdon't forget using System.Configuration;
      

  6.   

    System.Collections.Specialized.NameValueCollection appSettings = System.Configuration.ConfigurationSettings.AppSettings;string IP=appSettings["IP"];
    string Port=appSettings["Port"];
    string Name=appSettings["Name"];
      

  7.   

    TO:liujia_0421(SnowLover)
    在编译时下面这段提示找不到相关类或
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    是不是还有什么命名空间没有包括在里面?
      

  8.   

    可以在解决方案资源管理器的"引用"中添加引用,选择.NET标签中的System.Configuration就OK了.
    using System.Configuration;
    如果还不行把System.Configuration.Install这个也加上