简单读写XML文件

IPAddress.xml   文件如下:

<? xml version="1.0" encoding="utf-8" ?>
< IP >
      
< IPAddress > 192.168.0.120 </ IPAddress >
</ IP >

 

 

在 Form 窗体(读取XML配置.Designer.cs)中有如下控件:

复制代码
代码
         private   System.Windows.Forms.Button  button1;           // “读取”按钮
         private   System.Windows.Forms.Button  button2;           // “修改”按钮
         private   System.Windows.Forms.TextBox  textBox1;      //  用于显示和修改IP地址
         private   System.Windows.Forms.Label  label1;               //  显示“IP地址:”
         private   System.Windows.Forms.Button  button3;           //  “保存”按钮

复制代码

 

 

读取XML配置.cs  文件如下:

复制代码
代码
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
// myself
using  System.Xml;

namespace  CS_Test
{
    
public   partial   class  读取XML配置 : Form
    {
        
public  读取XML配置()
        {
            InitializeComponent();
        }

        XmlDocument doc 
=   null ;
        XmlNodeList nodeList 
=   null ;
        
string  path  = null ;
        
// 读取
         private   void  button1_Click( object  sender, EventArgs e)
        {
            doc 
=   new  XmlDocument();
            
// 读取文件地址
            path  =  Application.StartupPath.ToString();
            
// MessageBox.Show(path);
             int  n  =  path.LastIndexOf( " bin\\Debug " );
            path 
=  path.Substring( 0 , n);
            doc.Load((path 
+   " IPAddress.xml " ));
            MessageBox.Show(path 
+   " IPAddress.xml " );

            
if  (doc  !=   null )
            {
               nodeList 
=  doc.GetElementsByTagName( " IPAddress " );
               textBox1.Text 
=
                  nodeList[
0 ].FirstChild.Value.ToString();
            }
        }
        
// 修改
         private   void  button2_Click( object  sender, EventArgs e)
        {
            textBox1.ReadOnly 
=   false ;
        }
        
// 保存
         private   void  button3_Click( object  sender, EventArgs e)
        {
            
string  newIP  =  textBox1.Text.ToString();
            
if  (nodeList  !=   null )
            {
                
if  (textBox1.Text.Trim()  !=   "" )
                {
                    
// 通过根节点创建新元素
                    XmlNode n  =  doc.CreateTextNode(textBox1.Text.Trim());
                    
// 通过父节点替换子节点
                    nodeList[ 0 ].ReplaceChild(n,nodeList[ 0 ].FirstChild);
                    doc.Save(path 
+   " IPAddress.xml " );
                    MessageBox.Show(
" IP地址修改成功! " );
                    textBox1.ReadOnly 
=   true ;
                }
            }
        }
    }
    
class  ProgramXML
    {
        
static   void  Main( string [] args)
        {
            Application.Run(
new  读取XML配置());
        }
    }
}
复制代码

转载于:https://www.cnblogs.com/sunny_blog/archive/2012/07/09/2582424.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值