■ XmlSerializer 클래스 : XML 저장하기

----------------------------------------------------------------------------------------------------

using System.IO;

using System.Xml.Serialization;

 

#region XML 저장하기 - SaveXML(sourceObject, filePath)

 

/// <summary>

/// XML 저장하기

/// </summary>

/// <param name="sourceObject">소스 객체</param>

/// <param name="filePath">파일 경로</param>

public void SaveXML(object sourceObject, string filePath)

{

    StreamWriter streamWriter = null;

 

    try

    {

        string directoryPath = Path.GetDirectoryName(filePath);

 

        if(!Directory.Exists(directoryPath))

        {

            Directory.CreateDirectory(directoryPath);

        }

 

        streamWriter = new StreamWriter(filePath);

 

        XmlSerializer xmlSerializer = new XmlSerializer(sourceObject.GetType());

 

        xmlSerializer.Serialize(streamWriter, sourceObject);

    }

    finally

    {

        if(streamWriter != null)

        {

            streamWriter.Close();

 

            streamWriter = null;

        }

    }

}

 

#endregion

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요