用C#新建XML文件

 以前某项目中需要导出xml作为fusionChats数据源,xml貌似本身没有新建文件的函数,于是手写了一个方法

代码
   
     
using System.Text;
using System.IO;
using System.Xml;

namespace BIS.Service
{
public class FileCommand
{

public static readonly string DataPath = AppDomain.CurrentDomain.BaseDirectory + @" Data\ " ;
/// <summary>
/// 写出一个文件
/// </summary>
/// <param name="FileName"></param>
/// <param name="Content"></param>
/// <returns></returns>
public static bool CreateXML( string FileName, string Content)
{
bool isSuccess = false ;
// FileName += ".xml";
FileStream objFileStream = null ;
StreamWriter objStreamWriter
= null ;
try
{
// 判断文件是否已经存在,如果存在即覆盖
if (File.Exists(DataPath + FileName))
{
File.Delete(DataPath
+ FileName);
}
XmlDocument xml
= new XmlDocument();
objFileStream
= new FileStream(DataPath + FileName, FileMode.Append, FileAccess.Write);
objStreamWriter
= new StreamWriter(objFileStream, Encoding.Default);
objStreamWriter.WriteLine(
" <?xml version=\ " 1.0 \ " encoding=\ " utf - 8 \ " ?> " );
objStreamWriter.WriteLine(Content);
// 将字符串写入到文件中
isSuccess = true ;
}
catch (Exception e)
{
// throw new Exception("导出失败");
}
finally
{
if (objStreamWriter != null )
{
objStreamWriter.Flush();
objStreamWriter.Close();
}
}
return isSuccess;
}
}
}

 

你可能感兴趣的:(xml)