http://www.dotnetjohn.com/articles.aspx?articleid=173
using System.IO; using System.Xml; using System.Xml.Serialization; /// <summary> /// Method to convert a custom Object to XML string /// </summary> /// <param name="pObject">Object that is to be serialized to XML</param> /// <returns>XML string</returns> public String SerializeObject ( Object pObject ) { try { String XmlizedString = null; MemoryStream memoryStream = new MemoryStream ( ); XmlSerializer xs = new XmlSerializer ( typeof ( Animal ) ); XmlTextWriter xmlTextWriter = new XmlTextWriter ( memoryStream, Encoding.UTF8 );
xs.Serialize ( xmlTextWriter, pObject ); memoryStream = ( MemoryStream ) xmlTextWriter.BaseStream; XmlizedString = UTF8ByteArrayToString ( memoryStream.ToArray ( ) ); return XmlizedString; } catch ( Exception e ) { System.Console.WriteLine ( e ); return null; } } /// <summary> /// Method to reconstruct an Object from XML string /// </summary> /// <param name="pXmlizedString"></param> /// <returns></returns> public Object DeserializeObject ( String pXmlizedString ) { XmlSerializer xs = new XmlSerializer ( typeof ( Automobile ) ); MemoryStream memoryStream = new MemoryStream ( StringToUTF8ByteArray ( pXmlizedString ) ); XmlTextWriter xmlTextWriter = new XmlTextWriter ( memoryStream, Encoding.UTF8 );
return xs.Deserialize ( memoryStream ); } |
Serialize 시에 엔터 값를 그대로 받으려면 XmlTextWriter를 제외하고 사용.
XmlTextWriter를 사용 했을 때 xml 문자열이 한줄로 쭉 작성 됨.
'.NET > SampleCode' 카테고리의 다른 글
CreateUpdateQuery() (1) | 2008.04.28 |
---|---|
[BL] Insert 모듈 (1) | 2008.04.28 |