简体   繁体   English

如何在从具有工作“XmlInclude”属性的基类继承的类中包含“XmlInclude”?

[英]How do I include “XmlInclude” on a Class that inherits from a base class with working “XmlInclude” attributes?

I have multiple Projects in my solution and I am using overrides for any customization individual Clients request to keep code as organised as possible. 我在我的解决方案中有多个项目,我正在使用覆盖任何自定义单个客户端请求以保持代码尽可能有条理。 In this specific override I am trying to inherit from a base class for serialization I am going to do but I am getting errors when trying to serializing. 在这个特定的覆盖中,我试图从序列化的基类继承我将要做但我在尝试序列化时遇到错误。

//In the Base project serialization works 100%
namespace Template
{
    //[XmlInclude(typeof(StaticText))]
    //[XmlInclude(typeof(BoundText))]
    //[XmlInclude(typeof(StaticImage))]
    //[XmlInclude(typeof(BoundImage))]
    //[XmlInclude(typeof(Font))]
    public class objType
    {
        public float X { get; set; }
        public float Y { get; set; }
        public float Height { get; set; }
        public float Width { get; set; }
    }

    public class ObjMain()
    {
        public string ObjName { get; set; }
        public List<objType> ObjTypeItems { get; set; }
    }

    public class DoWork() {
        using (System.IO.TextReader tr = new System.IO.StringReader(templateXML))
        {
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ObjMain));
            bg = (ObjMain)serializer.Deserialize(tr);
        }
    }
}



/*
<ObjMain>
    <ObjName>Section 1</ObjName>
    <ObjTypeItems>
        <objType xsi:type="StaticText">
            <X>32.5</X>
            <Y>4.35</Y>
            <Height>10</Height>
            <Width>20</Width>
            <TextFieldName>M</TextFieldName>
        </objType> 
        <objType xsi:type="BountText">
            <X>32.5</X>
            <Y>4.35</Y>
            <Height>10</Height>
            <Width>20</Width>
            <TextFieldName>Mat</TextFieldName>
        </objType> 
    </ObjTypeItems>    
</ObjMain>
*/


namespace CustomTemplate
{
[XmlInclude(typeof(StaticText))]
    [XmlInclude(typeof(BoundText))]
    [XmlInclude(typeof(StaticImage))]
    [XmlInclude(typeof(BoundImage))]
    [XmlInclude(typeof(Font))]
    public class CustomObjType : objType
    {
        public string GroupName { get; set; }
    }

    public class CustomObjMain()
    {
        public string CObjName { get; set; }
        public List<CustomObjType> CObjTypeItems { get; set; }
    }

    public class DoWork() {
        using (System.IO.TextReader tr = new System.IO.StringReader(templateXML))
        {
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(CustomObjMain));//Error
            bg = (CustomObjMain)serializer.Deserialize(tr);
        }
    }

}

/*
<CustomObjMain>
    <CObjName>Section 1</CObjName>
    <ObjTypeItems>
        <CustomObjType xsi:type="StaticText">  <!--Error when with Serialization -->
            <X>32.5</X>
            <Y>4.35</Y>
            <Height>10</Height>
            <Width>20</Width>
            <TextFieldName>M</TextFieldName>
            <GroupName>MainGroup</GroupName>
        </CustomObjType> 
        <CustomObjType xsi:type="BountText">
            <X>32.5</X>
            <Y>4.35</Y>
            <Height>10</Height>
            <Width>20</Width>
            <TextFieldName>Bar</TextFieldName>
            <GroupName>MainGroup</GroupName>
        </CustomObjType> 
        <CustomObjType xsi:type="BountText">
            <X>32.5</X>
            <Y>4.35</Y>
            <Height>10</Height>
            <Width>20</Width>
            <TextFieldName>Foo</TextFieldName>
            <GroupName>SubGroupFoo</GroupName>
        </CustomObjType> 
    </ObjTypeItems>    
</CustomObjMain>
*/

My feeling is the class attributes is not inheriting with the base class. 我的感觉是类属性不是继承基类。 But I have also tried to add the attributes to the class that inherits from the base class but with no luck the error continues to show in the xml on the first line with the "xsi:type=" added to it. 但我也尝试将属性添加到从基类继承的类中,但没有运气,错误继续显示在第一行的xml中,并添加了“xsi:type =”。

Try following : 试试以下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication5
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            ObjMain objects = new ObjMain()
            {
                ObjName = "Main",
                objects = new List<ObjMain>() {
                    new objType1 { ObjName = "type 1" , X = 1.0F, Y = 2.0F, Height = 5F, Width = 10F},
                    new objType2 { ObjName = "type 2" , X = 1.5F, Y = 2.5F, Height = 5.5F, Width = 10.5F}
                }
            };

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(FILENAME, settings);
            XmlSerializer serializer = new XmlSerializer(typeof(ObjMain));
            serializer.Serialize(writer, objects);


        }
    }
    public class objType1 : ObjMain
    {
        public float X { get; set; }
        public float Y { get; set; }
        public float Height { get; set; }
        public float Width { get; set; }
    }
    public class objType2 : ObjMain
    {
        public float X { get; set; }
        public float Y { get; set; }
        public float Height { get; set; }
        public float Width { get; set; }
    }

    [XmlInclude(typeof(objType1))]
    [XmlInclude(typeof(objType2))]
    public class ObjMain
    {
        public string ObjName { get; set; }
        [XmlElement()]
        public List<ObjMain> objects { get; set; }
    }
}

暂无
暂无

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

相关问题 XmlInclude用于派生类而无需基类? - XmlInclude for derived class with no acess to base class? Web服务-在派生类而不是基类中包含XmlInclude? - Web services - XmlInclude in a derived class instead of a base class? XmlSerializer.Deserialize即使使用XmlInclude也不能与基类一起使用 - XmlSerializer.Deserialize doesn't work with the base class even with XmlInclude 没有[XMLInclude]或Type []构造函数的抽象类的序列化 - Serialization of an abstract class without [XMLInclude] or Type[] Constructor 有没有理由为什么用XmlInclude修饰的基类在序列化时仍会抛出类型未知异常? - Is there a reason why a base class decorated with XmlInclude would still throw a type unknown exception when serialized? 如何动态添加XmlInclude属性 - How to add XmlInclude attribute dynamically 如何使用XmlInclude序列化IEnumerable - How to use XmlInclude to serialize IEnumerable 如何在从基础 class 继承的 class 中编写 class 构造函数,其中基础 ZA2F2ED4F8EBC2CBB4C21AAB29DC4 需要注入服务? - How can I code a class constructor in a class that inherits from a base class where the base class needs a service injected? 如何使用XmlInclude动态指定类型? - How to use XmlInclude to specify types dynamically? 当我可以在继承自其的类中执行相同的操作时,为什么要在类中设置构造函数以设置基本参数? - Why set up a constructor in a class to set a base parameter when I could do the same in a class it inherits from?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM