0

I am trying to serialize an object that I have created here:

using (MemoryStream memStream = new MemoryStream())
{
    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(CaseTreatment));
    ser.WriteObject(memStream, ct);
}

This is ct here: List<CaseTreatment> ct = new List<CaseTreatment>();

Whenever my code hits ser.WriteObject(memStream, ct); I get the following error:

Type 'System.Collections.Generic.List`1[[FileUpload.Models.CaseTreatment, FileUpload,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name 
'ArrayOfCaseTreatment:http://schemas.datacontract.org/2004/07/FileUpload.Models' is 
not expected. Consider using a DataContractResolver or add any types not known 
statically to the list of known types - for example, by using the KnownTypeAttribute 
attribute or by adding them to the list of known types passed to 
DataContractSerializer.

My CaseTreatment object looks like this:

[DataContract]
public class CaseTreatment
{
    [DataMember]
    public Bridge BridgeTreatment;
}

And Bridge looks like this:

[DataContract]
public class Bridge : CaseGeneric
{
    [DataMember]
    public bool IsBridgeTreatment;

    public Bridge(XElement treatment, IEnumerable<XElement> orderDetails)
    {
        var tn = Util.GetTitle(treatment);
        this.Instruction = Util.GetInstruction(tn);
        this.Id = 2;
        this.Name= "Bridge";
        this.Something = Util.GetSomething(tn);
    }

    public Bridge()
    {

    }

}

Something is another object:

[DataContract]
public class Something
{
    [DataMember]
    public string Number;
}

And CaseGeneric has all of this:

[DataContract]
public class CaseGeneric
{
    [DataMember]
    public string Instruction;
    [DataMember]
    public int Id;
    [DataMember]
    public string Name;
    [DataMember]
    public List<Something> Something;
}

Would anyone be able to help? Also please let me know if you need more information, I will be more than happy to add/edit anything or answer any questions. I thought that I might need to decorate my objects with [KnownType(typeof(blah))] I just dont know where and of which one.

1 Answer 1

1

You are trying to write a object in a list. Try changing DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(CaseTreatment)); to DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<CaseTreatment>));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.