XmlSerializer中的动态ExpandoObject异常

时间:2017-05-25 08:47:12

标签: c# xml asp.net-mvc entity-framework expandoobject

我有一个类,在类中有一个属性类型动态列表,当我调用同一个类的序列化然后抛出异常 -

  

要成为XML可序列化,从IEnumerable继承的类型必须具有   Add(System.Object)在其所有级别的实现   继承层次结构。 System.Dynamic.ExpandoObject未实现   添加(System.Object的)。

类架构 -

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 = new XmlSerializer(TestClass.GetType());

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

2 个答案:

答案 0 :(得分:0)

列出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);
        }

答案 1 :(得分:-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();
    }
}