将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;
            }
        }

猜你喜欢

转载自www.cnblogs.com/nayilvyangguang/p/9981536.html