Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
        { 
            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

like image 302
Jan Ryll Avatar asked Mar 31 '10 11:03

Jan Ryll


1 Answers

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

like image 113
SwDevMan81 Avatar answered Oct 05 '22 03:10

SwDevMan81