博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 读写 xml
阅读量:6935 次
发布时间:2019-06-27

本文共 1897 字,大约阅读时间需要 6 分钟。

引用:

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配置());
        }
    }
}

转载地址:http://rfgjl.baihongyu.com/

你可能感兴趣的文章
微软发布Azure Application Insights for Node.js 1.0版本
查看>>
前端实例练习 - 动效按钮
查看>>
Java 20年:JVM虚拟化技术的发展
查看>>
Azure Service Fabric正式发布
查看>>
唐文:挖掘产品生命周期潜藏的商业价值——应用性能管理
查看>>
迅雷“星域”打通最后一公里重新定义CDN
查看>>
专访Matt Klein关于在Lyft构建Envoy的问答
查看>>
Intuit的Alex Balazs访谈
查看>>
Java社区领袖介绍平台支持选项
查看>>
JFinal整合Shiro(二)
查看>>
Concourse:可扩展的开源CI管道工具
查看>>
唯品会HDFS性能挑战和优化实践
查看>>
我所知道的flex布局 —— 上篇
查看>>
MongoDB主动撤回SSPL的开源许可申请
查看>>
梁胜:做云计算,如何才能超越AWS?
查看>>
微服务开源项目ServiceComb 毕业成为Apache顶级项目
查看>>
ThoughtWorks雷达上的新奇变化
查看>>
《可扩展的艺术》内容回顾与作者采访
查看>>
Java 9推迟6个月发布?
查看>>
Spark 2.4重磅发布:优化深度学习框架集成,提供更灵活的流式接收器
查看>>