Hakan Axheim

Hakan Axheim

  • 1.2k
  • 201
  • 29.4k

Problems with deserialize an XML string

Mar 8 2021 7:33 PM
Hi,
 
I want to deserialize an XML string into two classes. My main class is called Images se below and that class shall reference to another class called Image. As you can see I want a List of Image connected to the Images class. When I deserialize my XML string the List will not be assigned to the values in my XML string. I don't know why. Maybe someone can help me with this problem?
  1. namespace DataContractSample.DataContract.Model  
  2. {  
  3.     using System.Collections.Generic;  
  4.     using System.Runtime.Serialization;  
  5.   
  6.     [DataContract(Name="objects", Namespace = "", IsReference = true)]  
  7.     [KnownType(typeof(IList<Image>))]  
  8.   
  9.     public class Images  
  10.     {  
  11.         [DataMember(Name = "object")]  
  12.         public IList<Image> ImageList { getset; }  
  13.     }  
  14. }  
  15. namespace DataContractSample.DataContract.Model  
  16. {  
  17.     using System;  
  18.     using System.Runtime.Serialization;  
  19.     [DataContract(Name = "object", Namespace = "")]  
  20.     public class Image  
  21.    {  
  22.         [DataMember(Name = "id")]  
  23.         public Guid Id { getset; }  
  24.    }  
  25. }  
This is my XML string:
  1. <objects>  
  2. <object>  
  3. <id>CF7E7B22-1D3A-4CFE-91F9-F8C5C2DB4069</id>  
  4. </object>  
  5. <object>  
  6. <id>CF7E7B22-1D3A-4CFE-91F9-F8C5C2DB4070</id>  
  7. </object>  
  8. </objects>

Answers (2)