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();
        }

Feb 3, 2011

Exposing unreferenced data types via WCF service

For example, need to expose an enumeration that is not used by any of the WCF service operations to the client (via WSDL).

While ServiceKnownType attribute on the service class/interface/method exposed the type in the XSD schema of the WSDL, the default client proxy generation does not generate code for it.

Eventually ended up with a dummy solution of having a dummy method:


public class ExposedDataTypes
{
public CustomType1 type1 { get; set; }
public CustomType2 type2 { get; set; }
}



ExposedDataTypes IService.Ignore()
{
return null;
}


Not the best solution, but couldn't gracefully work around it. Easy and works nicely though.