操作XML的辅助类
FileName: XmlHelper.cs CLRVersion: 4.0.30319.18444 Author: Devin DateTime: 2016/1/25 13:17:09 GitHub: https://github.com/v5bep7/Utility
        private XmlElement GenerateServiceDefinition()
        {
            if (Source == null)
                Source = XmlHelper.ParseAsDOM("<Definition Type=\"JavaScript\"/>");

            if (Source.SelectSingleNode("Code") == null)
            {
                XmlHelper xh = new XmlHelper(Source);
                xh.AddElement("Code");
            }

            Source.SelectSingleNode("Code").InnerXml =((JSEditor)Editor).JavaScriptCode;

            return Source;
        }
        private void SaveToSite()
        {
            err.Clear();
            if (string.IsNullOrWhiteSpace(txtAccessPoint.Text))
            {
                err.SetError(txtAccessPoint, "主機位置不可空白");
                return;
            }

            XElement result = GetAppDeployElement();

            Connection con = new Connection();
            try
            {
                con.Connect(txtAccessPoint.Text, txtContract.Text, txtUserName.Text, txtPassword.Text);
                XmlHelper h = new XmlHelper(result.ToString(SaveOptions.None));
                con.SendRequest("Server.DeployApplication", new Envelope(h));
                con.SendRequest("LoadBalance.ReloadServer", new Envelope());
            }
            catch (Exception ex)
            {
                MessageBox.Show("部署時發生錯誤\n" + ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("儲存完成", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public void SetDefinition(XmlElement definition)
        {
            XmlHelper h = new XmlHelper(definition);

            dgExt.Rows.Clear();
            dgRestrict.Rows.Clear();

            foreach (XmlElement x in h.GetElements("Authentication/UserInfoProcessors/Processor/Restrict"))
            {
                int index = dgRestrict.Rows.Add();
                DataGridViewRow row = dgRestrict.Rows[index];
                row.Cells[colRestrictType.Name].Value = x.GetAttribute("Type");
                row.Cells[colCondition.Name].Value = ExtendAuthEditor.GetDisplay(x);
                row.Tag = x;
            }

            foreach (XmlElement x in h.GetElements("Authentication/UserInfoProcessors/Processor/ExtendProperty"))
            {
                int index = dgExt.Rows.Add();
                DataGridViewRow row = dgExt.Rows[index];
                row.Cells[dgExtType.Name].Value = x.GetAttribute("Type");
                row.Cells[dgExtDisplay.Name].Value = ExtendAuthEditor.GetDisplay(x);
                row.Tag = x;
            }
        }
        private void ImportFromSite()
        {
            err.Clear();
            if (string.IsNullOrWhiteSpace(txtAccessPoint.Text))
            {
                err.SetError(txtAccessPoint, "主機位置不可空白");
                return;
            }

            Connection connection = new Connection();
            connection.EnableSecureTunnel= true;
            try
            {
                connection.Connect(txtAccessPoint.Text, txtContract.Text, txtUserName.Text, txtPassword.Text);
            }
            catch (Exception ex)
            {
                err.SetError(txtAccessPoint, "主機連線失敗 : \n" + ex.Message);
                return;
            }

            XmlHelper h = new XmlHelper();
            h.AddElement(".","ApplicationName","shared");
            Envelope env = new Envelope(h);
            
            env = connection.SendRequest("Server.ExportApplication", env);
            h = new XmlHelper(env.Body);
            Console.WriteLine(h.XmlString);
            this.Import(h.GetElement("."));
        }
        public XmlElement GetAuthElement()
        {
            XmlHelper h = new XmlHelper("<Authentication />");
            h.SetAttribute(".", "Extends", Type.ToString());


            if (dgRestrict.Rows.Count > 0 || dgExt.Rows.Count > 0)
                h.AddElement(".", "UserInfoProcessors");

            if (dgRestrict.Rows.Count > 0)
            {
                XmlElement xml = h.AddElement("UserInfoProcessors", "Processor");
                xml.SetAttribute("Type", "restrict");

                XmlHelper xh = new XmlHelper(xml);
                foreach (DataGridViewRow row in this.dgRestrict.Rows)
                {
                    XmlElement res = row.Tag as XmlElement;
                    xh.AddElement(".", res);
                }
            }

            foreach (DataGridViewRow row in this.dgExt.Rows)
            {
                XmlElement xml = h.AddElement("UserInfoProcessors", "Processor");
                xml.SetAttribute("Type", "ExtendProperty");

                XmlHelper xh = new XmlHelper(xml);

                XmlElement res = row.Tag as XmlElement;
                xh.AddElement(".", res);
            }

            return h.GetElement(".");
        }