views:

140

answers:

2

Hi @all

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

A: 

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.

Jaxidian
No, adding [Serializable] wont work.
Jan Ryll
+2  A: 

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

SwDevMan81
+1, the problem is the attribute, it has nothing to do with the base class.
Hans Passant
Tanks! Seems to work!But why the serializer works if ommitting the base class and do not derive? Should the exception not also occur?
Jan Ryll