05 May 2005

XML Serialization

This is a sample of how to serialize a class in .Net

using System;

using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;

namespace WindowsApplication1
{
public class AppConfig
{
private long _Code;
private string _Description;
private string _Obs;
public long Code
{
get {return _Code;}
set { _Code = value;}
}

public string Description
{
get {return _Description;}
set { _Description = value;}
}

public string Obs
{
get {return _Obs;}
set { _Obs = value;}
}

}

///
/// Summary description for Configuration.
///

[XmlInclude(typeof(AppConfig))]
public class Configuration
{
private AppConfig _AppConfig = null;
public Configuration()
{
_AppConfig = new AppConfig();
}

public void SetConfiguration(long code, string desc, string obs)
{
_AppConfig.Code = code;
_AppConfig.Description = desc;
_AppConfig.Obs = obs;
}

public void Searialize()
{
XmlTextWriter writer = new XmlTextWriter("test.xml", Encoding.Unicode);
writer.Formatting = Formatting.Indented;
XmlSerializer serializer = new XmlSerializer(typeof(AppConfig));
serializer.Serialize(writer, _AppConfig);
writer.Close();
}
}
}


No comments: