views:

69

answers:

1

I used svcutil to generate classes to access the web service. I need to serialize them. The class is quite simple, here is how it looks

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="cosemAttributeDescriptor", Namespace="http://www.energiened.nl/Content/Publications/dsmr/P32")]
public partial class cosemAttributeDescriptor : object, System.Runtime.Serialization.IExtensibleDataObject
{

    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    private ushort ClassIdField;

    private string InstanceIdField;

    private sbyte AttributeIdField;

    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
    {
        get
        {
            return this.extensionDataField;
        }
        set
        {
            this.extensionDataField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
    public ushort ClassId
    {
        get
        {
            return this.ClassIdField;
        }
        set
        {
            this.ClassIdField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, EmitDefaultValue=false)]
    public string InstanceId
    {
        get
        {
            return this.InstanceIdField;
        }
        set
        {
            this.InstanceIdField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true, Order=2)]
    public sbyte AttributeId
    {
        get
        {
            return this.AttributeIdField;
        }
        set
        {
            this.AttributeIdField = value;
        }
    }
}

Now, the problem is I can't serialize the class. Here is the serialization code:

        cosemAttributeDescriptor cAttrD = new cosemAttributeDescriptor();
        cAttrD.ClassId = 3;
        cAttrD.InstanceId = "0100010800FF";
        cAttrD.AttributeId = 0;
        DataContractSerializer dSer = new DataContractSerializer(typeof(cosemAttributeDescriptor));
        StringBuilder sb = new StringBuilder();
        XmlWriter xW = XmlWriter.Create(sb);
        dSer.WriteObject(xW, cAttrD);

When I try to serialize the class, I get empty string. Any thoughts?

+1  A: 

I found the problem. The serialization in this way simply doesn't work, but following code has same effect.

DataContractSerializer dSer = new DataContractSerializer(typeof(cosemAttributeDescriptor));
MemoryStream mStr = new MemoryStream(); 
dSer.WriteObject(mStr, cAttrD); 
mStr.Position = 0; 
StreamReader sRead = new StreamReader(mStr); 
String result = sRead.ReadToEnd();
Bogi