简单的操作xml ,用于在winform中配置文件.

写内容到配置文件中.
try{
     ArrayList str = new ArrayList();
     XmlDocument xmlDoc = new XmlDocument();    
     xmlDoc.Load("config.xml"); //config.xml是一个配置文件
     XmlNode node = xmlDoc.DocumentElement;
     XmlNode appNote = node.SelectSingleNode("appSettings");
     XmlNodeList appNoteList = appNote.ChildNodes;
     str.Add(txtSqlServer.Text); //获取界面上的配置信息
     str.Add(txtUser.Text);
     str.Add(txtPassword.Text);
     str.Add(txtDb.Text);
     int i=0;
     foreach(XmlNode tmpNote in appNoteList)
     {
      tmpNote.Attributes["value"].Value = str[i].ToString(); //传到xml中.
      i++;
     }
     xmlDoc.Save("config.xml");
     this.Close();
    }
    catch(Exception ex)
    {
     MessageBox.Show(ex.Message);
   }
//读内容.
try{
     XmlDocument xmlDoc = new XmlDocument();    
     xmlDoc.Load("config.xml");
     XmlNode node = xmlDoc.DocumentElement;
     XmlNode appNote = node.SelectSingleNode("appSettings");
     XmlNodeList appNoteList = appNote.ChildNodes;
     MessageBox.Show(appNote.ChildNodes[2].Attributes["value"].Value);
     foreach(XmlNode tmpNote in appNoteList)
     {
      MessageBox.Show(tmpNote.Attributes["value"].Value);
     }
     xmlDoc.Save("config.xml");
     this.Close();
    }
    catch(Exception ex)
    {
     MessageBox.Show(ex.Message);
    }
config.xml中的内容.
<?xml version="1.0"?>
<config>
<appSettings>
    <add key="SQLServerIPorName" value="192.168.0.99" />
    <add key="SQLServerUser" value="sa" />
    <add key="SQLServerPwd" value="" />
    <add key="SQLServerDB" value="PowerCase" />
</appSettings>
</config>
我用的平台是SharpDevelop1.1的 XML文件是我添加的一个空的文件. 注意路径 config.xml 在debug 下面.

你可能感兴趣的:(xml,职场,WinForm,休闲)