简体   繁体   English

XmlSerializer中的动态ExpandoObject异常

[英]Exception in XmlSerializer for a Dynamic ExpandoObject

I have a Class and in class there is a property type Dynamic List when i am calling serializing for same class then threw exception- 我有一个类,在类中,当我为同一类调用序列化然后抛出异常时,有一个属性类型动态列表

To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. 要使XML可序列化,从IEnumerable继承的类型必须在其继承层次结构的所有级别上都具有Add(System.Object)的实现。 System.Dynamic.ExpandoObject does not implement Add(System.Object). System.Dynamic.ExpandoObject不实现Add(System.Object)。

Class architecture - 类架构-

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

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

List productList = new 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);
        }

Please use a model class instead of dynamic for serialization, since the type need to be defined for dynamic. 请使用模型类而不是动态类进行序列化,因为需要为动态定义类型。

All I did was assign proper values to every property and changed the TestClass.GetType() to obj.GetType() and print the textWriter. 我所做的就是为每个属性分配适当的值,然后将TestClass.GetType()更改为obj.GetType()并打印textWriter。 Please check below code : 请检查以下代码:

The ModelClass : ModelClass:

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

The TestClass : 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; }
}

The Class to execute the Program : 执行程序的类:

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM