程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#針對xml根本操作及保留設置裝備擺設文件運用實例

C#針對xml根本操作及保留設置裝備擺設文件運用實例

編輯:C#入門知識

C#針對xml根本操作及保留設置裝備擺設文件運用實例。本站提示廣大學習愛好者:(C#針對xml根本操作及保留設置裝備擺設文件運用實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#針對xml根本操作及保留設置裝備擺設文件運用實例正文


本文實例講述了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文檔

由於比擬簡略,直接寫辦法及成果。

public void CreateXMLDocument()
{
    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);        

//添加節點
    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");//保留的途徑
}

成果:

<?xml version="1.0" encoding="GB2312"?>
-<Employees>-

  <Node ISB="2-3631-4" Name="李明">

    <title>進修VS</title>-

    <price>

      <weight>20</weight>

    </price>

  </Node>

</Employees>

2 增長節點

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("D:\\book.xml");//找到xml文件
XmlNode root = xmlDoc.SelectSingleNode("Employees");//查找Employees節點
XmlElement xe1 = xmlDoc.CreateElement("Node2");//添加Node2節點
xe1.SetAttribute("Name", "張三");
XmlElement xeSub1 = xmlDoc.CreateElement("title");//界說子節點
xeSub1.InnerText = "心境好";
xe1.AppendChild(xeSub1);//添加節點到Node2
root.AppendChild(xe1);//添加節點到Employees
xmlDoc.Save("D:\\book.xml");

成果:

<?xml version="1.0" encoding="GB2312"?>
  -<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 修正節點:

public void ModifyNode()
{
    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Load("D:\\book.xml");

    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");
}

運轉成果:

<?xml version="1.0" encoding="GB2312"?>
-<Employees>
-<Node ISB="2-3631-4" Name="李明明">
<title>明天氣象欠好</title>-<price>
<weight>88</weight>
</price>
</Node>
-<Node2 Name="張三">
<title>心境好</title>
</Node2></Employees>

4 刪除節點:

public void DeleteNode()
{
    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);//刪除節點title
  }
     }
 }
    }

    xmlDocument.Save("D:\\book3.xml");
}

成果:
<?xml version="1.0" encoding="GB2312"?>
-<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的創立、節點的添加、節點的修正和刪除,上面以寫的一個保留項目設置裝備擺設文件的小例子。

舉例解釋:

起首在項目文件中創立一個xml文檔:

<?xml version="1.0" encoding="utf-8" ?>
<configurationN>
    <ServerAddress>1143</ServerAddress>
    <ID>192.168</ID>
  </configurationN>

在保留設置裝備擺設文件時,最重要應用了兩個辦法:Load和Save。

Load:初始化xml文檔,以便項目文件獲得詳細的xml節點的值。

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)
    { }
}

Save:在項目體系中停止修正設置裝備擺設文件值後,須要對xml停止從新保留

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)
    { }
}

此處將一切代碼都貼出來,便利下次完成。由於項目是WPF文件,並且都是簡略控件,所以只貼出後台代碼。

class ConfigurationManager:INotifyPropertyChanged
{
        public const string managerNode = "configurationN";//根節點
        public const string configuration_ServerAddress = "ServerAddress";//子節點

        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);
        }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved