private ServiceEntity(XmlElement definition)
        {
            XmlHelper h = new XmlHelper(definition);
            this.SQLTemplate = h.GetText("SQLTemplate");
            ResponseRecordElement = h.GetText("ResponseRecordElement");
            RequestRecordElement = h.GetText("RequestRecordElement");
            FieldList = new FieldList(h.GetElement("FieldList"));
            ConditionList = new ConditionList(h.GetElement("Conditions"));
            Orders = new OrderList(h.GetElement("Orders"));
            Pagination = new Pagination(h.GetElement("Pagination"));
           
            ServiceAction action = ServiceAction.Select;
            if (!Enum.TryParse<ServiceAction>(h.GetText("Action"), true, out action))
                action = ServiceAction.Select;
            Action = action;

            this.Variables = new List<IVariable>();
            foreach (XmlElement varElement in h.GetElements("InternalVariable/Variable"))
            {
                IVariable v = VariableFactory.Parse(varElement);
                if (v != null)
                    Variables.Add(v);
            }

            this.Converters = new List<IConverter>();
            foreach (XmlElement cvElement in h.GetElements("Converters/Converter"))
            {
                string type = cvElement.GetAttribute("Type");
                IConverter c = ConverterProvider.CreateConverter(type);
                c.Load(cvElement);

                this.Converters.Add(c);
            }

            this.Preprocesses = new List<Preprocess>();
            foreach (XmlElement preElement in h.GetElements("Preprocesses/Preprocess"))
            {
                Preprocess p = Preprocess.Parse(preElement);
                this.Preprocesses.Add(p);
            }
        }
        public XmlElement GetResult()
        {
            //TODO
            XmlHelper h = new XmlHelper("<Definition />");
            h.SetAttribute(".", "Type", "DBHelper");
            h.AddElement(".", "Action", ServiceAction.Select.ToString());
            
            XmlElement sqlTmp = h.AddElement(".", "SQLTemplate");
            XmlCDataSection section = sqlTmp.OwnerDocument.CreateCDataSection(this.txtSQLTemplate.Text);
            sqlTmp.AppendChild(section);

            h.AddElement(".", "ResponseRecordElement", txtRequestElement.Text);
            FieldList fields = new FieldList(this.txtFieldListName.Text, this.txtFieldListSource.Text);
            foreach (DataGridViewRow row in this.dgFieldList.Rows)
            {
                Field f = Field.Parse(row);
                fields.Fields.Add(f);
            }
            h.AddElement(".", fields.GetXml(ServiceAction.Select));

            ConditionList conditions = new ConditionList(this.txtConditionName.Text, this.txtConditionSource.Text,chkRequired.Checked);
            foreach (DataGridViewRow row in this.dgConditions.Rows)
            {
                Condition condition = Condition.Parse(row);
                conditions.Conditions.Add(condition);
            }
            h.AddElement(".", conditions.GetXml());

            OrderList orders = new OrderList(txtOrderName.Text, txtOrderSource.Text);
            foreach (DataGridViewRow row in dgOrder.Rows)
            {
                string target = row.Cells[colOrderTarget.Name].Value + string.Empty;
                string source = row.Cells[colOrderSource.Name].Value + string.Empty;
                Order o = new Order(target,source);
                orders.Orders.Add(o);
            }
            h.AddElement(".", orders.GetXml());

            int max = 0;
            if (!int.TryParse(txtMaxPageSize.Text, out max))
                max = 0;
            Pagination p = new Pagination(chkAllowPagination.Checked, max);
            h.AddElement(".", p.GetXml());

            if (this.Service.Variables.Count > 0)
            {
                XmlElement varElement = h.AddElement(".", "InternalVariable");                
                foreach (IVariable v in this.Service.Variables)                
                    h.AddElement("InternalVariable", v.GetXml());                
            }

            if (this.Service.Converters.Count > 0)
            {
                XmlElement cvElement = h.AddElement(".", "Converters");
                foreach (IConverter c in this.Service.Converters)
                    h.AddElement("Converters", c.Output());
            }

            if (this.dgProcessor.Rows.Count > 0)
            {
                XmlElement proElement = h.AddElement(".", "Preprocesses");
                foreach (DataGridViewRow row in this.dgProcessor.Rows)
                {
                    Preprocess pp = row.Tag as Preprocess;
                    if (pp == null) continue;

                    h.AddElement("Preprocesses", pp.GetXml());                    
                }
            }
            return h.GetElement(".");
        }