C#針對xml基本操作及保存配置文件應(yīng)用實(shí)例
本文實(shí)例講述了C#針對xml的基本操作及保存配置文件應(yīng)用,分享給大家供大家參考。具體方法如下:
引言:這里首先介紹了xml的基本操作,后面寫了一個(gè)經(jīng)常用到的xml保存配置文件的實(shí)例。
xml常用方法:
定義xml文檔:XmlDocument xmlDoc = new XmlDocument();
初始化xml文檔:xmlDoc.Load("D:\\book.xml");//找到xml文件
創(chuàng)建根元素:XmlElement xmlElement = xmlDoc.CreateElement("", "Employees", "");
創(chuàng)建節(jié)點(diǎn):XmlElement xeSub1 = xmlDoc.CreateElement("title");
查找Employees節(jié)點(diǎn):XmlNode root = xmlDoc.SelectSingleNode("Employees");
添加節(jié)點(diǎn):xe1.AppendChild(xeSub1);
更改節(jié)點(diǎn)的屬性:xe.SetAttribute("Name", "李明明");
移除xe的ID屬性:xe.RemoveAttribute("ID");
刪除節(jié)點(diǎn)title:xe.RemoveChild(xe2);
1 創(chuàng)建xml文檔
因?yàn)楸容^簡單,直接寫方法及結(jié)果。
{
XmlDocument xmlDoc = new XmlDocument();
//加入XML的聲明段落,<?xml version="1.0" encoding="gb2312"?>
XmlDeclaration xmlDeclar;
xmlDeclar = xmlDoc.CreateXmlDeclaration("1.0", "gb2312", null);
xmlDoc.AppendChild(xmlDeclar);
//加入Employees根元素
XmlElement xmlElement = xmlDoc.CreateElement("", "Employees", "");
xmlDoc.AppendChild(xmlElement);
//添加節(jié)點(diǎn)
XmlNode root = xmlDoc.SelectSingleNode("Employees");
XmlElement xe1 = xmlDoc.CreateElement("Node");
xe1.SetAttribute("Name", "李明");
xe1.SetAttribute("ISB", "2-3631-4");
//添加子節(jié)點(diǎn)
XmlElement xeSub1 = xmlDoc.CreateElement("title");
xeSub1.InnerText = "學(xué)習(xí)VS";
xe1.AppendChild(xeSub1);
XmlElement xeSub2 = xmlDoc.CreateElement("price");
xe1.AppendChild(xeSub2);
XmlElement xeSub3 = xmlDoc.CreateElement("weight");
xeSub3.InnerText = "20";
xeSub2.AppendChild(xeSub3);
root.AppendChild(xe1);
xmlDoc.Save("D:\\book.xml");//保存的路徑
}
結(jié)果:
-<Employees>-
<Node ISB="2-3631-4" Name="李明">
<title>學(xué)習(xí)VS</title>-
<price>
<weight>20</weight>
</price>
</Node>
</Employees>
2 增加節(jié)點(diǎn)
xmlDoc.Load("D:\\book.xml");//找到xml文件
XmlNode root = xmlDoc.SelectSingleNode("Employees");//查找Employees節(jié)點(diǎn)
XmlElement xe1 = xmlDoc.CreateElement("Node2");//添加Node2節(jié)點(diǎn)
xe1.SetAttribute("Name", "張三");
XmlElement xeSub1 = xmlDoc.CreateElement("title");//定義子節(jié)點(diǎn)
xeSub1.InnerText = "心情好";
xe1.AppendChild(xeSub1);//添加節(jié)點(diǎn)到Node2
root.AppendChild(xe1);//添加節(jié)點(diǎn)到Employees
xmlDoc.Save("D:\\book.xml");
結(jié)果:
-<Employees>
-<Node ISB="2-3631-4" Name="李明">
<title>學(xué)習(xí)VS</title>-
<price>
<weight>20</weight>
</price>
</Node>-
<Node2 Name="張三">
<title>心情好</title>
</Node2>-
<Node2 Name="張三">
<title>心情好</title>
</Node2>
</Employees>
3 修改節(jié)點(diǎn):
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("D:\\book.xml");
XmlNodeList nodeList = xmlDocument.SelectSingleNode("Employees").ChildNodes;//獲取Employees節(jié)點(diǎn)的所有子節(jié)點(diǎn)
foreach (XmlNode xn in nodeList)//遍歷
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("Name") == "李明")
{
xe.SetAttribute("Name", "李明明");//更改節(jié)點(diǎn)的屬性
XmlNodeList xnl = xe.ChildNodes;//獲取xe的所有子節(jié)點(diǎn)
foreach (XmlNode xn1 in xnl)
{
XmlElement xe2 = (XmlElement)xn1;//將節(jié)點(diǎn)xn1的屬性轉(zhuǎn)換為XmlElement
if (xe2.Name == "title")//找到節(jié)點(diǎn)名字為title的節(jié)點(diǎn)
{
xe2.InnerText = "今天天氣不好";
}
if (xe2.Name == "price")
{
XmlNodeList xnl2 = xe2.ChildNodes;
foreach (XmlNode xn2 in xnl2)
{
if (xn2.Name == "weight")
{
xn2.InnerText = "88";
}
}
}
}
}
}
xmlDocument.Save("D:\\book2.xml");
}
運(yùn)行結(jié)果:
-<Employees>
-<Node ISB="2-3631-4" Name="李明明">
<title>今天天氣不好</title>-<price>
<weight>88</weight>
</price>
</Node>
-<Node2 Name="張三">
<title>心情好</title>
</Node2></Employees>
4 刪除節(jié)點(diǎn):
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("D:\\book1.xml");
XmlNodeList xnl = xmlDocument.SelectSingleNode("Employees").ChildNodes;
foreach (XmlNode xn in xnl)
{
if (xn.Name == "Node")
{
XmlElement xe = (XmlElement)xn;//將xn的屬性轉(zhuǎn)換為XmlElement
xe.RemoveAttribute("ID");//移除xe的ID屬性
XmlNodeList xnl2 = xe.ChildNodes;
for (int i = 0; i < xnl2.Count; i++)
{
XmlElement xe2 = (XmlElement)xnl2.Item(i);
if (xe2.Name == "title")
{
xe.RemoveChild(xe2);//刪除節(jié)點(diǎn)title
}
}
}
}
xmlDocument.Save("D:\\book3.xml");
}
結(jié)果:
-<Employees>
-<Node ISB="2-3631-4" Name="李明">-<price>
<weight>20</weight>
</price>
</Node>-
<Node2 Name="張三">
<title>心情好</title>
</Node2>-
<Node2 Name="張三">
<title>心情好</title>
</Node2>
</Employees>
前面介紹了xml的創(chuàng)建、節(jié)點(diǎn)的添加、節(jié)點(diǎn)的修改和刪除,下面以寫的一個(gè)保存項(xiàng)目配置文件的小例子。
舉例說明:
首先在項(xiàng)目文件中創(chuàng)建一個(gè)xml文檔:
<configurationN>
<ServerAddress>1143</ServerAddress>
<ID>192.168</ID>
</configurationN>
在保存配置文件時(shí),最主要使用了兩個(gè)方法:Load和Save。
Load:初始化xml文檔,以便項(xiàng)目文件獲取具體的xml節(jié)點(diǎn)的值。
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlNodeList xnl = xmlDocument.SelectSingleNode(managerNode).ChildNodes;
foreach (XmlNode xn in xnl)
{
if (xn.Name == configuration_ServerAddress)
{
ServerAddress = xn.InnerText;
}
}
}
catch(Exception ex)
{ }
}
Save:在項(xiàng)目系統(tǒng)中進(jìn)行修改配置文件值后,需要對xml進(jìn)行重新保存
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlNodeList xnl = xmlDocument.SelectSingleNode(managerNode).ChildNodes;
foreach (XmlNode xn in xnl)
{
if (xn.Name == configuration_ServerAddress)
{
xn.InnerText = ServerAddress;
}
}
xmlDocument.Save(path);
}
catch (Exception ex)
{ }
}
此處將所有代碼都貼出來,方便下次實(shí)現(xiàn)。因?yàn)轫?xiàng)目是WPF文件,而且都是簡單控件,所以只貼出后臺(tái)代碼。
{
public const string managerNode = "configurationN";//根節(jié)點(diǎn)
public const string configuration_ServerAddress = "ServerAddress";//子節(jié)點(diǎn)
private string _ServerAddress;
public string ServerAddress
{
get { return _ServerAddress; }
set
{
_ServerAddress = value;
NotifyPropertyChanged("ServerAddress");
}
}
public void Load(string path)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlNodeList xnl = xmlDocument.SelectSingleNode(managerNode).ChildNodes;
foreach (XmlNode xn in xnl)
{
if (xn.Name == configuration_ServerAddress)
{
ServerAddress = xn.InnerText;
}
}
}
catch(Exception ex)
{ }
}
public void Save(string path)
{
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(path);
XmlNodeList xnl = xmlDocument.SelectSingleNode(managerNode).ChildNodes;
foreach (XmlNode xn in xnl)
{
if (xn.Name == configuration_ServerAddress)
{
xn.InnerText = ServerAddress;
}
}
xmlDocument.Save(path);
}
catch (Exception ex)
{ }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public static ConfigurationManager Instance = new ConfigurationManager();
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Start();
this.tb1.Text = ConfigurationManager.Instance.ServerAddress.ToString();
}
private string path = "CONFIG\\System.xml";
private void button1_Click(object sender, RoutedEventArgs e)
{
ConfigurationManager.Instance.ServerAddress = this.tb1.Text;
ConfigurationManager.Instance.Save(path);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void Start()
{
ConfigurationManager.Instance.Load(path);
}
}
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)程序等待延遲執(zhí)行的方法
這篇文章主要介紹了C#實(shí)現(xiàn)程序等待延遲執(zhí)行的方法,涉及C#動(dòng)態(tài)鏈接庫的使用及延遲的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-09-09C#實(shí)現(xiàn)控制電腦注銷,關(guān)機(jī)和重啟
這篇文章主要為大家介紹了C#如何實(shí)現(xiàn)控制電腦注銷,關(guān)機(jī)和重啟功能,本案例涉及的知識點(diǎn)包含:Process、Shell32.dll、User32.dll、Struct數(shù)據(jù)結(jié)構(gòu),感興趣的可以了解一下2022-09-09C#使用List類實(shí)現(xiàn)動(dòng)態(tài)變長數(shù)組的方法
這篇文章主要介紹了C#使用List類實(shí)現(xiàn)動(dòng)態(tài)變長數(shù)組的方法,涉及C#中List類的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04解決unity3d導(dǎo)入模型貼圖材質(zhì)丟失的問題
這篇文章主要介紹了解決unity3d導(dǎo)入模型貼圖材質(zhì)丟失的問題,具有很好的參考價(jià)值,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04unity使用socket編程實(shí)現(xiàn)聊天室功能
這篇文章主要為大家詳細(xì)介紹了unity使用socket編程實(shí)現(xiàn)聊天室功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11