Tuesday, April 12, 2005

Serializing an Object to a XML document!

Pretty elementary but cool concept of Serializing an object to XMLDocument that i read about on the net somewhere... This gives you serialized 'Web Service' like output for virtually any object that is serializable!!!
...
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Data;

namespace com.miswaco.OracleApplicationsWebServices.ServiceFramework
{
///
/// Additional XML Functionality to serialize objects to XML Documents!
///

public class CustomXMLHelper
{
public CustomXMLHelper() {}
public static XmlDocument GetobjAsXmlDocument(object obj)
{
XmlDocument retVal = new XmlDocument();
//Convert the obj into an Xml document
XmlSerializer ser = new XmlSerializer(obj.GetType());
StringBuilder sb = new StringBuilder();
TextWriter writer = new StringWriter(sb);
ser.Serialize(writer, obj );
StringReader reader = new StringReader(writer.ToString());
//Read the Xml from the StringReader object
DataSet dst = new DataSet();
dst.ReadXml(reader);
retVal.LoadXml(dst.GetXml());
return retVal;
}
}
}

0 Comments:

Post a Comment

<< Home