本文實例講述了C#針對xml的基本操作及保存配置文件應用,分享給大家供大家參考。具體方法如下:
引言:這里首先介紹了xml的基本操作,后面寫了一個經常用到的xml保存配置文件的實例。
xml常用方法:
定義xml文檔:XmlDocument xmlDoc = new XmlDocument();
初始化xml文檔:xmlDoc.Load("D://book.xml");//找到xml文件
創建根元素:XmlElement xmlElement = xmlDoc.CreateElement("", "Employees", "");
創建節點:XmlElement xeSub1 = xmlDoc.CreateElement("title");
查找Employees節點:XmlNode root = xmlDoc.SelectSingleNode("Employees");
添加節點:xe1.AppendChild(xeSub1);
更改節點的屬性:xe.SetAttribute("Name", "李明明");
移除xe的ID屬性:xe.RemoveAttribute("ID");
刪除節點title:xe.RemoveChild(xe2);
1 創建xml文檔
因為比較簡單,直接寫方法及結果。
//加入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);
//添加節點
XmlNode root = xmlDoc.SelectSingleNode("Employees");
XmlElement xe1 = xmlDoc.CreateElement("Node");
xe1.SetAttribute("Name", "李明");
xe1.SetAttribute("ISB", "2-3631-4");
//添加子節點
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");//保存的路徑
}
結果:
<Node ISB="2-3631-4" Name="李明">
<title>學習VS</title>-
<price>
<weight>20</weight>
</price>
</Node>
</Employees>
2 增加節點
結果:
-<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 修改節點:
XmlNodeList nodeList = xmlDocument.SelectSingleNode("Employees").ChildNodes;//獲取Employees節點的所有子節點
foreach (XmlNode xn in nodeList)//遍歷
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("Name") == "李明")
{
xe.SetAttribute("Name", "李明明");//更改節點的屬性
XmlNodeList xnl = xe.ChildNodes;//獲取xe的所有子節點
foreach (XmlNode xn1 in xnl)
{
XmlElement xe2 = (XmlElement)xn1;//將節點xn1的屬性轉換為XmlElement
if (xe2.Name == "title")//找到節點名字為title的節點
{
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");
}
運行結果:
4 刪除節點:
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);//刪除節點title
}
}
}
}
xmlDocument.Save("D://book3.xml");
}
結果:
前面介紹了xml的創建、節點的添加、節點的修改和刪除,下面以寫的一個保存項目配置文件的小例子。
舉例說明:
首先在項目文件中創建一個xml文檔:
在保存配置文件時,最主要使用了兩個方法:Load和Save。
Load:初始化xml文檔,以便項目文件獲取具體的xml節點的值。
XmlNodeList xnl = xmlDocument.SelectSingleNode(managerNode).ChildNodes;
foreach (XmlNode xn in xnl)
{
if (xn.Name == configuration_ServerAddress)
{
ServerAddress = xn.InnerText;
}
}
}
catch(Exception ex)
{ }
}
Save:在項目系統中進行修改配置文件值后,需要對xml進行重新保存
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)
{ }
}
此處將所有代碼都貼出來,方便下次實現。因為項目是WPF文件,而且都是簡單控件,所以只貼出后臺代碼。
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.VeVB.COm/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.VeVB.COm/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.VeVB.COm/code/xml_format_compress
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答