將XML字串寫入到XML檔案並儲存到指定路徑
阿新 • • 發佈:2018-11-19
/// <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;
}
}