Esempio n. 1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            CommendService commendService = new CommendService();
            commendService.Guid = txtGuid.Text.Trim();
            commendService.Name = txtName.Text.Trim();
            commendService.Enable = chkEnable.Checked;
            commendService.Description = txtDescription.Text.Trim();
            commendService.LogoUrl = txtLogoUrl.Text.Trim();
            BusinessType businessType = new BusinessType();
            businessType.Name = cboBusinessTypeName.Text.Trim();
            businessType.CommentService.Add(commendService);

            ResultInfo resultInfo = XMLManager.InserRecordIntoXml(path, businessType);
            if (!resultInfo.IsSuccessed)
            {
                MessageBox.Show(resultInfo.ReturnMessage, resultInfo.ReturnCode);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 向指定的xml文档中插入记录
        /// </summary>
        /// <param name="path"></param>
        /// <param name="businessType"></param>
        public static ResultInfo InserRecordIntoXml(string path, BusinessType businessType)
        {
            ResultInfo resultInfo = null;
            FileInfo xmlFile = new FileInfo(path);

            //如果文件不存在
            if (!xmlFile.Exists)
            {
                resultInfo = CreateXmlFile(path, businessType);
            }
            else
            {
                //如果文件存在,判断是加到那个Businesstype下
                XElement xElementRoot = XElement.Load(path);
                XmlOperation xmlOperation = XmlOperation.None;

                //遍历判断业务名是否存在
                foreach (XElement xElementType in xElementRoot.Element("BusinessTypes").Elements("BusinessType"))
                {
                    if (xElementType.Attribute("Name").Value.ToString() == businessType.Name)
                    {
                        xmlOperation = XmlOperation.BusinessTypeExist;

                        //遍历判断服务名是否存在
                        foreach (XElement xElementService in xElementType.Element("CommendServices").Elements("CommendService"))
                        {
                            if (xElementService.Attribute("Name").Value.ToString() == businessType.CommentService[0].Name)
                            {
                                //服务名已经存在
                                xmlOperation = XmlOperation.CommendServiceExist;
                                resultInfo = ResultInfoFactory<ResultInfo>.Create(false, "ServiceExit", "CommendService名称已存在!");
                                break;
                            }
                            else
                                xmlOperation = XmlOperation.CommendService;
                        }

                        //如果服务不存在,则创建
                        if (xmlOperation == XmlOperation.CommendService)
                            resultInfo = CreateCommendServiceNode(xElementType, businessType.CommentService[0]);
                        break;
                    }
                    else
                        xmlOperation = XmlOperation.BusinessType;
                }

                //如果业务不存在,则创建节点
                if (xmlOperation == XmlOperation.BusinessType)
                {
                    resultInfo = CreateBusinessTypeNode(xElementRoot, businessType);
                }

                try
                {
                    xElementRoot.Save(path);
                }
                catch (Exception ex)
                {
                    resultInfo = ResultInfoFactory<ResultInfo>.Create(true, "SaveException", "保存信息时发生异常:\r\t {0}", ex.Message);
                }
            }
            return resultInfo;
        }
Esempio n. 3
0
 /// <summary>
 /// 新建xml文档
 /// </summary>
 /// <param name="path">文件保存的路径</param>
 /// <param name="businessType">业务实体</param>
 private static ResultInfo CreateXmlFile(string path, BusinessType businessType)
 {
     ResultInfo resultInfo = null;
     try
     {
         XDocument xmlDoc = new XDocument(
                       new XDeclaration("1.0", "utf-8", "no"),
                       new XElement("XmlCommendService",
                           new XElement("BusinessTypes",
                               new XElement("BusinessType",
                                   new XAttribute("Name", businessType.Name),
                                   new XElement("CommendServices",
                                       new XElement("CommendService",
                                           new XAttribute("Guid", businessType.CommentService[0].Guid),
                                           new XAttribute("Name", businessType.CommentService[0].Name),
                                           new XAttribute("Enable", businessType.CommentService[0].Enable.ToString()),
                                           new XAttribute("Description", businessType.CommentService[0].Description),
                                           new XAttribute("LogoUrl", businessType.CommentService[0].LogoUrl)
                                           ))))));
         xmlDoc.Save(path);
         resultInfo = ResultInfoFactory<ResultInfo>.Create(true, "XDocumentSuccessed", "Xml创建成功");
     }
     catch (Exception ex)
     {
         resultInfo = ResultInfoFactory<ResultInfo>.Create(false, "XDocumentException", "Xml创建时异常:\r\t{1}", ex.Message);
     }
     return resultInfo;
 }
Esempio n. 4
0
 /// <summary>
 /// 创建业务节点
 /// </summary>
 /// <param name="xElementParent">该业务在父节点</param>
 /// <param name="businessType">业务实体</param>
 private static ResultInfo CreateBusinessTypeNode(XElement xElementParent, BusinessType businessType)
 {
     ResultInfo resultInfo = null;
     try
     {
         XElement newElement = new XElement("BusinessType",
                                    new XAttribute("Name", businessType.Name),
                                    new XElement("CommendServices",
                                        new XElement("CommendService",
                                            new XAttribute("Guid", businessType.CommentService[0].Guid),
                                                new XAttribute("Name", businessType.CommentService[0].Name),
                                                new XAttribute("Enable", businessType.CommentService[0].Enable.ToString().ToLower()),
                                                new XAttribute("Description", businessType.CommentService[0].Description),
                                                new XAttribute("LogoUrl", businessType.CommentService[0].LogoUrl)
                                                )));
         xElementParent.Element("BusinessTypes").Add(newElement);
         resultInfo = ResultInfoFactory<ResultInfo>.Create(true, "BusinessSuccessed", "业务节点 {0} 创建成功", businessType.Name);
     }
     catch (Exception ex)
     {
         resultInfo = ResultInfoFactory<ResultInfo>.Create(false, "BusinessException", "业务节点 {0} 创建时异常:\r\t{1}", businessType.Name, ex.Message);
     }
     return resultInfo;
 }