将XML字符串写入到XML文件并保存到指定路径

前端之家收集整理的这篇文章主要介绍了将XML字符串写入到XML文件并保存到指定路径前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
          /// <summary>
         /// 将XML字符串写入到XML文件
         /// </summary>
        /// <param name="strXML"></param>
        private int WriteStrToXML(string strXmlName, string strXML)
        {
            try
            {
                string filepath = System.Web.HttpContext.Current.Server.MapPath("~/CatitalXML");
                if (!Directory.Exists(filepath))
                    Directory.CreateDirectory(filepath);
                filepath = filepath + "/" + DateTime.Now.ToString("yyyy-MM-ddHHmmss") + strXmlName + ".XML";
                XmlDocument xdoc = new XmlDocument();
                xdoc.AppendChild(xdoc.CreateXmlDeclaration("1.0""UTF-8"null));//xml文件
                xdoc.LoadXml(strXML);
                xdoc.Save(filepath);
                return 0;
            }
            catch (Exception)
            {
                return -1;
            }
        }

猜你在找的XML相关文章