Ejemplo n.º 1
0
        /// <summary>
        /// 检查文件是否存在,如不存在是否创建文件夹及XML的文件头
        /// </summary>
        /// <param name="strFileName">检查的文件名称</param>
        /// <param name="CreatePath">是否创建文件夹</param>
        /// <param name="CreateXMLHead">是否创建XML文件头</param>
        /// <returns>文件是否存在</returns>
        public static bool PrepareFileExist(string strFileName, bool bCreatePath, bool bCreateXMLHead)
        {
            if (File.Exists(strFileName))
            {
                return(true);
            }

            string str = Path.GetDirectoryName(strFileName);

            if (str != strFileName)
            {
                if (Directory.Exists(str) == false)
                {
                    if (bCreatePath)
                    {
                        Directory.CreateDirectory(str);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            if (bCreateXMLHead)
            {
                CXmlFile.CreateXmlFile(strFileName);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public static void SaveStringListToXmlFile(List <string> lst, string strXMLFileName, string node)
        {
            if (PrepareFileExist(strXMLFileName, true, true) == false)
            {
                throw new Exception(string.Format("创建文件:{0}  出错!\r\n错误位置:{1}",
                                                  strXMLFileName, "SaveStringListToXmlFile"));
            }

            XmlDocument xmldoc = new XmlDocument();

            xmldoc.Load(strXMLFileName);
            string  nodeName = node;
            XmlNode root     = xmldoc.SelectSingleNode("/config");

            root = CXmlFile.CreateElementEx(xmldoc, root, nodeName);
            if (root == null)
            {
                throw new Exception(string.Format("创建节点是出错!\r\n操作文件:{1}\r\n错误位置:{0}",
                                                  "SaveStringListToXmlFile", strXMLFileName));
            }
            else
            {
                root.RemoveAll();
            }

            foreach (string value in lst)
            {
                XmlElement doc = xmldoc.CreateElement("Item");
                doc.InnerText = value;
                root.AppendChild(doc);
            }
            xmldoc.Save(strXMLFileName);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 得到XML文件中节点的值
        /// </summary>
        /// <param name="strXMLFileName">xml文件名</param>
        /// <param name="node">xml内的节点名称 多级节点使用"/"分隔</param>
        public static List <string> GetStringListFromXmlFile(string strXMLFileName, string node, List <string> lst = null)
        {
            List <string> result = new List <string>();

            if (PrepareFileExist(strXMLFileName, true, true) == false)
            {
                throw new Exception(string.Format("创建文件:{0}  出错!\r\n错误位置:{1}",
                                                  strXMLFileName, "SaveStringListToXmlFile"));
            }

            XmlDocument xmldoc = new XmlDocument();

            xmldoc.Load(strXMLFileName);
            string  nodeName = node;
            XmlNode root     = xmldoc.SelectSingleNode("/config/" + nodeName);

            if (root == null)
            {
                root = xmldoc.SelectSingleNode("/config");
                root = CXmlFile.CreateElementEx(xmldoc, root, nodeName);
                if (lst != null)
                {
                    foreach (string value in lst)
                    {
                        XmlElement doc = xmldoc.CreateElement("Item");
                        doc.InnerText = value;
                        root.AppendChild(doc);
                    }
                }
                xmldoc.Save(strXMLFileName);
                return(lst);
            }

            XmlNodeList mm = root.ChildNodes;

            foreach (XmlNode mNode in mm)
            {
                result.Add(mNode.InnerText);
            }

            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 保存信息到XML文件
        /// </summary>
        /// <param name="str">内容</param>
        /// <param name="strXMLFileName">xml文件名</param>
        /// <param name="node">xml内的节点名称 多级节点使用"/"分隔</param>
        public static void SaveStringToXmlFile(string str, string strXMLFileName, string node)
        {
            if (PrepareFileExist(strXMLFileName, true, true) == false)
            {
                if (CreateXmlFile(strXMLFileName) == false)
                {
                    throw new Exception(string.Format("创建文件:{0}  出错!\r\n错误位置:{1}",
                                                      strXMLFileName, "SaveStringListToXmlFile"));
                }
            }

            XmlDocument xmldoc = new XmlDocument();

            xmldoc.Load(strXMLFileName);
            string  nodeName = node;
            XmlNode root     = xmldoc.SelectSingleNode("/config");

            root = CXmlFile.CreateElementEx(xmldoc, root, nodeName);
            if (root == null)
            {
                try
                {
                    XmlWrite(strXMLFileName, node, str);
                }
                catch (Exception)
                {
                    throw new Exception(string.Format("创建节点是出错!\r\n操作文件:{1}\r\n错误位置:{0}",
                                                      "SaveStringListToXmlFile", strXMLFileName));
                }
            }
            else
            {
                root.RemoveAll();
            }
            root.InnerText = str;
            xmldoc.Save(strXMLFileName);
        }