Use C # To create an XML file

Source: Internet
Author: User

In the past, xml needs to be exported as the fusionChats data source in a project. xml does not seem to have a function to create a new file, so a method is handwritten.

Code

 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>
/// Write an object
/// </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
{
// Determine whether the file already exists. If yes, overwrite the file.
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); // write the string to the file.
IsSuccess = true;
}
Catch (Exception e)
{
// Throw new Exception ("Export failed ");
}
Finally
{
If (objStreamWriter! = Null)
{
ObjStreamWriter. Flush ();
ObjStreamWriter. Close ();
}
}
Return isSuccess;
}
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.