2017-05-25 45 views
0

我有一個類,並在課堂上有,當我打電話序列化的同一類然後把exception-異常的XmlSerializer的一個動態ExpandoObject

要XML序列化,其繼承的類型屬性類型動態列表從IEnumerable必須有 繼承層次結構的所有級別的Add(System.Object)實現。 System.Dynamic.ExpandoObject不執行 添加(System.Object)。

Class架構 -

public class TestClass 
{ 
    public string Property1{ get; set; } 
    public string Property2{ get; set; } 
    public string Property3{ get; set; } 
    public string Property4{ get; set; } 

    public List<dynamic> ProductList { get; set; } 
} 

XmlSerializer的XmlSerializer的新= XmlSerializer的(TestClass.GetType());

 using (StringWriter textWriter = new StringWriter()) 
     { 
      xmlSerializer.Serialize(textWriter, Obj); 
      string xmlString=textWriter.ToString(); 
     } 

回答

-1

請使用一個模型類,而不是動態的序列化,因爲類型需要動態進行定義。

我所做的每一個屬性分配正確的價值觀,改變了TestClass.GetType()來obj.GetType()和打印的TextWriter。請檢查下面的代碼:

的ModelClass:

public class ModelClass 
{ 
    public string Name { get; set; } 
    public string Search { get; set; } 
} 

的識別TestClass:

public class TestClass 
{ 
    public string Property1 { get; set; } 
    public string Property2 { get; set; } 
    public string Property3 { get; set; } 
    public string Property4 { get; set; } 

    public List<ModelClass> ProductList { get; set; } 
} 

的類執行程序:

class Program 
{ 
    public static void Main(string[] args) 
    { 
     List<ModelClass> productList = new List<ModelClass>(); 

     var product1 = new ModelClass(); 
     product1.Name = "Name1"; 
     product1.Search = "Search1"; 

     productList.Add(product1); 

     var product2 = new ModelClass(); 
     product2.Name = "Name2"; 
     product2.Search = "Search2"; 

     productList.Add(product2); 

     var product3 = new ModelClass(); 
     product3.Name = "Name3"; 
     product3.Search = "Search3"; 

     productList.Add(product3); 

     TestClass obj = new TestClass(); 

     obj.Property1 = "p1"; 
     obj.Property2 = "p2"; 
     obj.Property3 = "p3"; 
     obj.Property4 = "p4"; 
     obj.ProductList = productList; 

     XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType()); 

     using (StringWriter textWriter = new StringWriter()) 
     { 
      xmlSerializer.Serialize(textWriter, obj); 
      string xmlString = textWriter.ToString(); 
      Console.WriteLine(textWriter); 
     } 

     Console.ReadKey(); 
    } 
} 
+0

好的謝謝。但我的情況產品列表的屬性不是一個字符串列表。 請檢查我的回答 – amethianil

+0

@amethianil你仍然面臨例外嗎? – ViVi

0

List productList = new List();

 dynamic product1 = new ExpandoObject(); 
     product1.Name = "Name1"; 
     product1.Search = "Search1"; 

     productList.Add(product1); 

     dynamic product2 = new ExpandoObject(); 
     product2.Name = "Name2"; 
     product2.Search = "Search2"; 

     productList.Add(product2); 

     dynamic product3 = new ExpandoObject(); 
     product3.Name = "Name3"; 
     product3.Search = "Search3"; 

     productList.Add(product3); 

     TestClass obj = new TestClass(); 

     obj.Property1 = "p1"; 
     obj.Property2 = "p2"; 
     obj.Property3 = "p3"; 
     obj.Property4 = "p4"; 
     obj.ProductList = productList; 

     XmlSerializer xmlSerializeraa = new XmlSerializer(obj.GetType()); 

     using (StringWriter textWriter = new StringWriter()) 
     { 
      xmlSerializeraa.Serialize(textWriter, obj); 
      string xmlStringOut = textWriter.ToString(); 
      Console.WriteLine(textWriter); 
     }