XML 追加節點 帶縮進

Form1.Designer.cs

using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;


namespace XMLNode
{
    partial class Form1
    {
        /// <summary>
        /// 必需的設計器變量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應釋放託管資源,爲 true;否則爲 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗體設計器生成的代碼

        /// <summary>
        /// 設計器支持所需的方法 - 不要修改
        /// 使用代碼編輯器修改此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(236, 91);
            this.Name = "Form1";
            this.Text = "Successful";
            this.ResumeLayout(false);

        }

        #endregion


        private void InsertNode()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"D:\Test.xml");
            var root = xmlDoc.DocumentElement;
            XmlComment newComm = xmlDoc.CreateComment("OPC UA Data Server");
            XmlElement newNode = xmlDoc.CreateElement("DataServerInfo"); 
            newNode.SetAttribute("CLSID", "{6A36E912-66C7-4E7F-AA1C-7C8B091885D3}");
            newNode.SetAttribute("DefaultPort", "7155");
            XmlElement xesub1 = xmlDoc.CreateElement("image");
            root.AppendChild(newComm);
            root.AppendChild(newNode);  
                       
            Console.WriteLine(xmlDoc);

            using (StreamWriter sw = new StreamWriter(@"D:\Test.xml"))
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.IndentChars = "  ";
                settings.NewLineOnAttributes = true;
                XmlWriter xmlWriter = XmlWriter.Create(sw, settings);
                xmlDoc.Save(xmlWriter);
                xmlWriter.Close();
            }
        }


        private void GetNode()
        {
            XmlDocument xmlDoc = new XmlDocument();
           // xmlDoc.Load(@"D:\Test.xml");
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            XmlReader reader = XmlReader.Create(@"D:\Test.xml", settings);
            xmlDoc.Load(reader);

            XmlNode xn = xmlDoc.SelectSingleNode("ServerList");
            XmlNodeList xnl = xn.ChildNodes;
            foreach (XmlNode node in xnl)
            {
                string str = node.Attributes["CLSID"].Value;
                if ( str.Equals(BASE_CLSID))
                {
                    return;
                    reader.Close();
                }

            }
            reader.Close();
            InsertNode();
        }

        private void GetExistsXML()
        {
            if (File.Exists(@XML_PATH))
            {
                string[] str = command;
                GetNode();
            }
            else
            {
                return;
            }

        }
    }
}
program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XMLNode
{
    static class Program
    {
        /// <summary>
        /// 應用程序的主入口點。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if(args.Length == 0)
            {
                Application.Run(new Form1());
            }
            else
            {
                Application.Run(new Form1(args));
            }
        }
    }
}
form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XMLNode
{
    public partial class Form1 : Form
    {
        public  const string BASE_CLSID = "{6A36E912-66C7-4E7F-AA1C-7C8B091885D3}";
        public const string XML_PATH = "D:\\Test.xml";
        public string[] command = null;
        public Form1()
        {
            InitializeComponent();
            GetExistsXML();

        }
    string[] args = null;
        public Form1(string[] args)
        {
            this.args = args;
            command = args;
            InitializeComponent();
            GetExistsXML();
            
        }
    }
    
}

<?xml version="1.0" encoding="utf-8"?>
<ServerList>
  <!-- OPC -->
  <DataServerInfo
    CLSID="{6A36E912-66C7-4E7F-AA1C-7C8B034380000}"
    DefaultPort="7133" />
  <!-- OPC -->
  <DataServerInfo
    CLSID="{6A36E912-66C7-4E7F-AA1C-7C8B093400001}"
    DefaultPort="7145" />
  <DataServerInfo
    CLSID="{6A36E912-66C7-4E7F-AA1C-7C83430910002}"
    DefaultPort="73435" />
  <DataServerInfo
    CLSID="{6A36E912-66C7-4E7F-AA1C-7C8B091000003}"
    DefaultPort="7155" />
  <DataServerInfo
    CLSID="{6A36E912-66C7-4E7F-AA1C-7C8B091800004}"
    DefaultPort="7155" />
  <DataServerInfo
    CLSID="{6A36E912-66C7-4E7F-AA1C-7C8B091880005}"
    DefaultPort="7155" />
  <!--OPC UA Data Server-->
  <DataServerInfo
    CLSID="{6A36E912-66C7-4E7F-AA1C-7C8B091885D3}"
    DefaultPort="7155" />
</ServerList>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章