简单读写XML文件
IPAddress.xml 文件如下:
<?xml version="1.0" encoding="utf-8"?>
<IP>
<IPAddress>192.168.0.120</IPAddress>
</IP>
<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; // “保存”按钮
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配置());
}
}
}
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配置());
}
}
}
作者: XuGang 网名:钢钢 |
出处: http://xugang.cnblogs.com |
声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址! |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架