C#針對xml基本操作及保存配置文件應用實例
本文實例講述了C#針對xml的基本操作及保存配置文件應用,分享給大家供大家參考。具體方法如下:
引言:這里首先介紹了xml的基本操作,后面寫了一個經(jīng)常用到的xml保存配置文件的實例。
xml常用方法:
定義xml文檔:XmlDocument xmlDoc = new XmlDocument();
初始化xml文檔:xmlDoc.Load("D:\\book.xml");//找到xml文件
創(chuàng)建根元素:XmlElement xmlElement = xmlDoc.CreateElement("", "Employees", "");
創(chuàng)建節(jié)點:XmlElement xeSub1 = xmlDoc.CreateElement("title");
查找Employees節(jié)點:XmlNode root = xmlDoc.SelectSingleNode("Employees");
添加節(jié)點:xe1.AppendChild(xeSub1);
更改節(jié)點的屬性:xe.SetAttribute("Name", "李明明");
移除xe的ID屬性:xe.RemoveAttribute("ID");
刪除節(jié)點title:xe.RemoveChild(xe2);
1 創(chuàng)建xml文檔
因為比較簡單,直接寫方法及結果。
{
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é)點
XmlNode root = xmlDoc.SelectSingleNode("Employees");
XmlElement xe1 = xmlDoc.CreateElement("Node");
xe1.SetAttribute("Name", "李明");
xe1.SetAttribute("ISB", "2-3631-4");
//添加子節(jié)點
XmlElement xeSub1 = xmlDoc.CreateElement("title");
xeSub1.InnerText = "學習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");//保存的路徑
}
結果:
-<Employees>-
<Node ISB="2-3631-4" Name="李明">
<title>學習VS</title>-
<price>
<weight>20</weight>
</price>
</Node>
</Employees>
2 增加節(jié)點
xmlDoc.Load("D:\\book.xml");//找到xml文件
XmlNode root = xmlDoc.SelectSingleNode("Employees");//查找Employees節(jié)點
XmlElement xe1 = xmlDoc.CreateElement("Node2");//添加Node2節(jié)點
xe1.SetAttribute("Name", "張三");
XmlElement xeSub1 = xmlDoc.CreateElement("title");//定義子節(jié)點
xeSub1.InnerText = "心情好";
xe1.AppendChild(xeSub1);//添加節(jié)點到Node2
root.AppendChild(xe1);//添加節(jié)點到Employees
xmlDoc.Save("D:\\book.xml");
結果:
-<Employees>
-<Node ISB="2-3631-4" Name="李明">
<title>學習VS</title>-
<price>
<weight>20</weight>
</price>
</Node>-
<Node2 Name="張三">
<title>心情好</title>
</Node2>-
<Node2 Name="張三">
<title>心情好</title>
</Node2>
</Employees>
3 修改節(jié)點:
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("D:\\book.xml");
XmlNodeList nodeList = xmlDocument.SelectSingleNode("Employees").ChildNodes;//獲取Employees節(jié)點的所有子節(jié)點
foreach (XmlNode xn in nodeList)//遍歷
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("Name") == "李明")
{
xe.SetAttribute("Name", "李明明");//更改節(jié)點的屬性
XmlNodeList xnl = xe.ChildNodes;//獲取xe的所有子節(jié)點
foreach (XmlNode xn1 in xnl)
{
XmlElement xe2 = (XmlElement)xn1;//將節(jié)點xn1的屬性轉換為XmlElement
if (xe2.Name == "title")//找到節(jié)點名字為title的節(jié)點
{
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");
}
運行結果:
-<Employees>
-<Node ISB="2-3631-4" Name="李明明">
<title>今天天氣不好</title>-<price>
<weight>88</weight>
</price>
</Node>
-<Node2 Name="張三">
<title>心情好</title>
</Node2></Employees>
4 刪除節(jié)點:
{
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的屬性轉換為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é)點title
}
}
}
}
xmlDocument.Save("D:\\book3.xml");
}
結果:
-<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é)點的添加、節(jié)點的修改和刪除,下面以寫的一個保存項目配置文件的小例子。
舉例說明:
首先在項目文件中創(chuàng)建一個xml文檔:
<configurationN>
<ServerAddress>1143</ServerAddress>
<ID>192.168</ID>
</configurationN>
在保存配置文件時,最主要使用了兩個方法:Load和Save。
Load:初始化xml文檔,以便項目文件獲取具體的xml節(jié)點的值。
{
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:在項目系統(tǒng)中進行修改配置文件值后,需要對xml進行重新保存
{
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)
{ }
}
此處將所有代碼都貼出來,方便下次實現(xiàn)。因為項目是WPF文件,而且都是簡單控件,所以只貼出后臺代碼。
{
public const string managerNode = "configurationN";//根節(jié)點
public const string configuration_ServerAddress = "ServerAddress";//子節(jié)點
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:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
希望本文所述對大家的C#程序設計有所幫助。
相關文章
C# 獲取指定QQ頭像繪制圓形頭像框GDI(Graphics)的方法
某論壇的評論區(qū)模塊,發(fā)現(xiàn)這功能很不錯,琢磨了一晚上做了大致一樣的,用來當做 注冊模塊 的頭像綁定功能,下面通過實例代碼給大家介紹下C# 獲取指定QQ頭像繪制圓形頭像框GDI(Graphics)的方法,感興趣的朋友一起看看吧2021-11-11
C#中如何自定義配置上周和本周起始日來查詢業(yè)務數(shù)據(jù)(思路詳解)
在C#中并沒有封裝的方法根據(jù)我們需要來直接獲取上一周某天到某天、本周某天到某天,所以需要我們自己封裝方法來實現(xiàn)(我們也可以按照這個思路使用其他語言來實現(xiàn)),感興趣的朋友跟隨小編一起看看吧2023-09-09
解析在內(nèi)部循環(huán)中Continue外部循環(huán)的使用詳解
本篇文章是對在內(nèi)部循環(huán)中Continue外部循環(huán)的使用進行了詳細的分析介紹,需要的朋友參考下2013-05-05

