locked
DataContractSerialization - View the serialized data RRS feed

  • Question

  • I am trying DataContractSerialization for the first time and I would like to see the results of my serialization. Some fields seem to be missing, and it is difficult trying to debug it without being able to see what is being serialized.

    I have tried both the CreateBinaryWriter and tried the CreateTextWriter with the hope I could view what it serialized:

        Dim str As New MemoryStream()
        Dim dcs As New DataContractSerializer(GetType(CustomerAccount))
    
        'Using w As XmlDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(str)
        '  dcs.WriteObject(w, Me)
        'End Using
        Using w As XmlDictionaryWriter = XmlDictionaryWriter.CreateTextWriter(str)
          dcs.WriteObject(w, Me)
          Debug.WriteLine(w.ToString())
        End Using
    

    In either case, I cannot find a way to view the results with actually deserializing back to an object.

    (I have used the XMLSerializer in the past and it was always easy to see the result of the serialization without actually deserializing.)

    Any tips would be appreciated.

     

     


    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Wednesday, August 18, 2010 8:11 PM

Answers

  • Hi,

    Here's what I used last week to do this:

    private string Serialize(List<Data2RootAct> obj)
    {
     DataContractSerializer ser = new DataContractSerializer(obj.GetType());
    
     string tmp = "";
     using (var sw = new StringWriter())
     {
      using (var xw = new XmlTextWriter(sw))
      {
       ser.WriteObject(xw, obj);
      }
      tmp = sw.ToString();
     }
     return tmp;
    }
    

    Hope that helps,

    Tom

    • Marked as answer by DeborahKMVP Tuesday, August 24, 2010 3:16 PM
    Tuesday, August 24, 2010 12:02 PM

All replies

  • Hi,

    Here's what I used last week to do this:

    private string Serialize(List<Data2RootAct> obj)
    {
     DataContractSerializer ser = new DataContractSerializer(obj.GetType());
    
     string tmp = "";
     using (var sw = new StringWriter())
     {
      using (var xw = new XmlTextWriter(sw))
      {
       ser.WriteObject(xw, obj);
      }
      tmp = sw.ToString();
     }
     return tmp;
    }
    

    Hope that helps,

    Tom

    • Marked as answer by DeborahKMVP Tuesday, August 24, 2010 3:16 PM
    Tuesday, August 24, 2010 12:02 PM
  • I'll give that a try. Thanks!
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, August 24, 2010 3:16 PM