Example #1
0
        //private List<XmlModelCustom> ToSortChildNodeaaa(List<XmlModelCustom> allXmlNodeList, List<XmlModelCustom> rootXmlList, XmlNodeList xmlList)
        //{
        //    try
        //    {

        //    }
        //    catch (Exception ex)
        //    {
        //         throw new Exception("HandleXmlMgr-->ToSortChildNode" + ex.Message, ex);
        //    }
        //}







    

        /// <summary>
        /// 獲取屬性相關信息
        /// </summary>
        /// <param name="xn">node節點</param>
        /// <returns>包含Attributes相關信息的XmlModeCustom對象</returns>
        private XmlModelCustom GetAttributes(XmlElement xn, XmlModelCustom xmlModelTemp)
        {
            
            string attris = string.Empty;
            string attrisXml = string.Empty;
            try
            {
                if (xn != null && xn.HasAttributes)
                {
                    ///是否有屬性
                    xmlModelTemp.hasAttributes = true;
                    ///獲得屬性集合
                    XmlAttributeCollection attributeList = xn.Attributes;
                    ///遍歷屬性
                    foreach (XmlAttribute attriTemp in attributeList)
                    {
                        ///如果有屬性 = index_id
                        if (attriTemp.Name == "index_id")
                        {
                            ///將該index_id 當做索引id
                            xmlModelTemp.rowId = Convert.ToInt32(attriTemp.Value);
                        }
                        ///累加attris
                        
                        attris += attriTemp.Name + "=" + attriTemp.Value + "|";
                        attrisXml += attriTemp.Name + "=\"" + attriTemp.Value + "\" "; 
                    }
                    ///將屬性賦予XmlModeCustom對象
                    xmlModelTemp.attributes = attris;
                    xmlModelTemp.attributesXml = attrisXml;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("HandleXmlMgr-->GetAttributes" + ex.Message,ex);
            }

            return xmlModelTemp;
        }
Example #2
0
        private List<XmlModelCustom> ToSort(XmlNodeList xmlList,List<XmlModelCustom> sortList)
        {
            try
            {

                foreach (XmlNode xn in xmlList)
                {
                    if (xn.InnerText.IndexOf("version=\"1.0\"") >= 0 || xn.Name == "#text"||xn.Name=="#comment")
                    {
                        continue;
                    }
                    XmlModelCustom xTemp = new XmlModelCustom();
                    xTemp.isTopNode = xn.ParentNode.Name == "#document" ? true : false;///設置是否是根節點
                    if (xn.Attributes != null)
                    {
                        xTemp = GetAttributes((XmlElement)xn, xTemp);///設置和屬性相關內容
                    }
                    xTemp.fileName = this.fileName;
                    xTemp.name = xn.Name;///設置Node節點的名稱
                    xTemp.text = xn.Name;
                    xTemp.parentName = xn.ParentNode.Name;///設置父節點名稱
                    xTemp.isLastNode = xn.InnerText == xn.InnerXml ? true : false;///設置是否是最後節點(當InnerText屬性==InnerXml屬性時,可證明為最後節點)
                    xTemp.code = xTemp.isLastNode == true ? xn.InnerText : string.Empty;///設置節點中內容.只有最後節點才能有內容
                    if (xTemp.isLastNode == true && xTemp.code != "") xTemp.text = xTemp.code;
                    if (xTemp.isTopNode == true) xTemp.xmlStr = xmlStr;
                    sortList.Add(xTemp);
                    if (xn.HasChildNodes)
                    {
                        XmlNodeList xList = xn.ChildNodes;
                        xTemp.children = ToSort(xList, xTemp.children);
                    }
                }

                return sortList;
            }
            catch (Exception ex)
            {
                throw new Exception("HandleXmlMgr-->ToSortChildNode" + ex.Message, ex);
            }
        }
Example #3
0
 /// <summary>
 /// 操作文件夾,讀取文件夾下的文件名稱
 /// </summary>
 /// <param name="theFolder">DirectoryInfo 對象</param>
 /// <returns>包含xml文件名的字符集</returns>
 private List<XmlModelCustom> HandleXmlFolder(DirectoryInfo theFolder)
 {
     try
     {
         if (theFolder == null) return new List<XmlModelCustom>(); ///如果為空直接返回list
         int indexTemp = 1;
         List<XmlModelCustom> list = new List<XmlModelCustom>();
         foreach (FileInfo NextFile in theFolder.GetFiles())///遍歷文件
         {
             XmlModelCustom xmcTemp = new XmlModelCustom();///實例化XmlModelCustom
             xmcTemp.rowId = indexTemp;
             xmcTemp.fileName = NextFile.Name;///獲得xml文件名稱
             xmcTemp.baseUrl = this.baseUrl;
             indexTemp++;///通過+1,保證索引不會重複
             list.Add(xmcTemp);///list集合添加
         }
         return list;///返回名字集合
     }
     catch (Exception ex)
     {
         throw new Exception("HandleXmlMgr-->HandleXmlFolder" + ex.Message, ex);
     }
 }
Example #4
0
        private bool UpdateNode(XmlDocument doc,XmlModelCustom xmc,string fullPath,int type)
        {
            try
            {

                
                int index_id = 0;
                ///獲得xmlDoc的所有節點
                XmlNodeList listXml = doc.GetElementsByTagName("*");
                ///定義一個handleXn變量
                XmlNode handleXn = null;
                ///循環遍歷節點,獲得選中節點
                foreach (XmlNode xn in listXml)
                {
                    ///獲得要修改的節點
                    if (index_id == xmc.rowId)
                    {
                        handleXn = xn;
                        break;
                    }
                    index_id++;
                }

                ///刪除本節點
                if(type==3)
                {
                    handleXn.ParentNode.RemoveChild(handleXn);

                    doc.Save(fullPath);

                    return true;
                }


                ///創建一個新節點
                 XmlElement newNode;

                 if (type == 1)
                 {
                     newNode = doc.CreateElement(xmc.brotherName);
                 }
                 else if (type == 2)
                 {
                     newNode = doc.CreateElement(xmc.childName);
                 }
                 else
                 {
                     newNode = doc.CreateElement(xmc.name);
                     
                 }
                ///分割屬性
                string [] attrArray = xmc.attributes.Split('|');

                ///設置屬性
                foreach(string attr in attrArray)
                {
                    if(attr!="")
                    {
                        string [] temp = attr.Split('=');
                        newNode.SetAttribute(temp[0],temp[1]);
                    }
                }

                ///設置內容
                //newNode.InnerText = xmc.code;

                if (type == 0)
                {
                    newNode = (XmlElement)LoopAddNode(newNode, handleXn);
                }

                ///添加新節點
                if (type == 1)
                {
                    handleXn.ParentNode.AppendChild(newNode);
                }
                if (type == 2)
                {
                    handleXn.AppendChild(newNode);
                }

                ///如果是修改節點
                if (type == 0)
                {
                    ///添加新節點
                    handleXn.ParentNode.AppendChild(newNode);
                    ///刪除掉舊節點
                    handleXn.ParentNode.RemoveChild(handleXn);
                }
                ///保存
                doc.Save(fullPath);

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("HandleXmlMgr-->UpdateNode" + ex.Message,ex);
            }
            
        }
Example #5
0
 public bool SaveOrUpdate(string fullPath,XmlModelCustom xmc,int type)
 {
     try
     {
         XmlDocument xmlDoc = LoadXmlFolder(fullPath);
         return UpdateNode(xmlDoc,xmc,fullPath,type);
     }
     catch (Exception ex)
     {
         throw new Exception("HandleXmlMgr-->UpdateNode"+ ex.Message,ex);
     }
 }
 public ActionResult UpdateNode()
 {
     try
     {
         XmlModelCustom xmc = new XmlModelCustom();
         int type = Request["type"] == null ? 0 : Convert.ToInt32(Request["type"]);
         xmc.rowId = Request["rowId"] == null ? 0 : Convert.ToInt32(Request["rowId"]);
         xmc.fileName = Request["fileName"];
         xmc.name = Request["name"].Trim();
         xmc.code = Request["code"].Trim();
         xmc.brotherName = Request["brotherName"].Trim();
         xmc.attributes = Request["attributes"].Trim();
         xmc.childName = Request["childName"].Trim(); 
         IHandleXmlImplMgr _xmlInfo = new HandleXmlMgr();
         string path = parentPath + xmc.fileName;
         path = Server.MapPath(path);
         bool flag=false;
         ///如果為4 刪除文件,則直接調用刪除文件方法
         if (type == 4)
         {
             flag = _xmlInfo.CreateFile(path, xmc.name);
             return Json(new { success = flag });
         }
         flag = _xmlInfo.SaveOrUpdate(path, xmc, type);
         return Json(new { success = flag });
     }
     catch (Exception ex)
     {
         Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage();
         logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message);
         logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
         log.Error(logMessage);
         return new EmptyResult();
     }
 }