日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

简单读写XML文件

發布時間:2023/12/9 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单读写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配置());
????????}
????}
}



本文轉自鋼鋼博客園博客,原文鏈接:http://www.cnblogs.com/xugang/archive/2010/04/17/1714199.html,如需轉載請自行聯系原作者

總結

以上是生活随笔為你收集整理的简单读写XML文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。