简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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