Feb 4, 2011

Serialize and Deserialize object to DB / XElement


//To serialize into XElement:

        XmlSerializer x = new XmlSerializer(typeof(ComplexType));
        XDocument doc = new XDocument();
ComplexType ct = _getComplexType();
using (XmlWriter xw = doc.CreateWriter())
{
ComplexType ct = _getComplexType();
x.Serialize(xw, complexType);
xw.Close();
}
XElement el = doc.Root;

//To deserialize into ComplexType:

        using (XmlReader xr = el.CreateReader())
        {
            ComplexType deserializedComplexType =
x.Deserialize(xr) as ComplexType;
            xr.Close();
        }

No comments:

Post a Comment