代码之家  ›  专栏  ›  技术社区  ›  111111

派生类的C#XML序列化

  •  13
  • 111111  · 技术社区  · 14 年前

    嗨,我正在尝试序列化一个从类派生的对象数组,我一直在用c#遇到同样的错误。非常感谢您的帮助。

    显然这个例子已经被缩小了,因为在现实世界中,这个帖子的形状会包含大量不同的形状。

    namespace XMLInheritTests
    {
        class Program
        {
            static void Main(string[] args)
            {
                Shape[] a = new Shape[1] { new Square(1) };
    
                FileStream fS = new FileStream("C:\\shape.xml",
                                            FileMode.OpenOrCreate);
                XmlSerializer xS = new XmlSerializer(a.GetType());
                Console.WriteLine("writing");
                try
                {
                    xS.Serialize(fS, a);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.InnerException.ToString());
                    Console.ReadKey();
                }
                fS.Close();
                Console.WriteLine("Fin");
            }
        }
    }
    

    形状.cs

    namespace XMLInheritTests
    {
        public abstract class Shape
        {
            public Shape() { }
            public int area;
            public int edges;
        }
    }
    

    方块字.cs

    namespace XMLInheritTests
    {
        public  class  Square : Shape
        {
            public int iSize;
            public Square() { }
    
            public Square(int size)
            {
                iSize = size;
                edges = 4;
                area = size * size;
            }
        }
    }
    

    错误: System.InvalidOperationException:不应为XMLInheritTests.Square类型。使用xmlclude或SoapInclude属性指定静态未知的类型。

    位于Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterShapeA

    非常感谢

    2 回复  |  直到 14 年前
        1
  •  25
  •   Marc Gravell    14 年前
    [XmlInclude(typeof(Square))]
    public abstract class Shape {...}
    

    XmlSerializer 构造器,但是:那就是 缓存和重用序列化程序实例;否则,您将使动态创建的程序集大出血。当您使用只接受 Type ,但不适用于其他重载。

        2
  •  3
  •   111111    14 年前

    解决方案:

    class Program
        {
            static void Main(string[] args)
            {
                Shape[] a = new Shape[2] { new Square(1), new Triangle() };
    
                FileStream fS = new FileStream("C:\\shape.xml",FileMode.OpenOrCreate);
    
                //this could be much cleaner
                Type[] t = { a[1].GetType(), a[0].GetType() };
    
    
                XmlSerializer xS = new XmlSerializer(a.GetType(),t);
                Console.WriteLine("writing");
                try
                {
                    xS.Serialize(fS, a);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.InnerException.ToString());
                    Console.ReadKey();
                }
                fS.Close();
                Console.WriteLine("Fin");
            }
        }
    
    namespace XMLInheritTests
    {
        [XmlInclude(typeof(Square))]
        [XmlInclude(typeof(Triangle))]
        public abstract class Shape
        {
            public Shape() { }
            public int area;
            public int edges;
        }
    }