开发者

XmlSerializer constructor error with class derived from a base class

the following code specifies a type "MyBase64Binary" which is derived from a base class "TestBase"

using System;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;

namespace Test
{
    public class TestBase
    {
        public TestBase()
        {
        }
    }

    [XmlType(TypeName = "base64Binary"), Serializable]
    public partial class MyBase64Binary : TestBase
    {
        [System.Xml.Serialization.XmlTextAttribute(DataType = "base64Binary")]
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public Byte[] __Value;

        [XmlIgnore]
        public Byte[] Value
        { 
  开发者_JS百科          get { return __Value; }
            set { __Value = value; }
        }

        public MyBase64Binary()
        {
        }

    }
}

If i try to create a XmlSerializer like this

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

namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer s = new XmlSerializer(typeof(Test.MyBase64Binary));
        }
    }
}

from this one then i get a InvalidOperationException Error:

{"There was an error reflecting type 'Test.MyBase64Binary'."}

And the Inner Exception tells me following:

{"Cannot serialize object of type 'Test.MyBase64Binary'. Consider changing type of XmlText member 'Test.MyBase64Binary.__Value' from System.Byte[] to string or string array."}

If I not derive from the "TestBase" class then all works fine.

I do not get the solution. Please help.

What's wrong?

Greetings from Germany

Jan


If you change the XmlTextAttribute to XmlAttribute or XmlElement it should be ok. Since you were trying to use the XmlTextAttribute, it assumed it would be some sort of string. If you want an actual byte array serialized, try the XmlAttribute or XmlElement


Does adding [Serializable] to your base class help? I'd look into making sure your base class is also properly decorated. I don't know if this will help or not, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜