c# - 从基类派生的类的 XmlSerializer 构造函数错误

标签 c# .net xml serialization xmlserializer

以下代码指定了从基类“TestBase”派生的类型“MyBase64Binary”

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()
        {
        }

    }
}

如果我尝试像这样创建一个 XmlSerializer

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

从这个然后我得到一个 InvalidOperationException 错误:

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

内部异常告诉我以下内容:

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

如果我不是从“TestBase”类派生的,那么一切正常。

我没有得到解决方案。请帮忙。
怎么了?

来自德国的问候

最佳答案

如果将 XmlTextAttribute 更改为 XmlAttribute 或 XmlElement,应该没问题。由于您尝试使用 XmlTextAttribute,它假定它是某种字符串。如果您想要序列化实际的字节数组,请尝试 XmlAttribute 或 XmlElement

关于c# - 从基类派生的类的 XmlSerializer 构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2552382/

相关文章:

c# - 为什么\n 在 Log.Info 方法中不起作用?

java - 使用 JAXB 解析响应 XML

java - 用于登录验证的xml文件

c# - XML 文档 (2, 2) 中存在错误。这是什么意思?

c# - 如何通过 Presenter 中的界面将我的 View 按钮数据绑定(bind)到模型

c# - 如何在数据库中存储这种类型的类关系?

java - 解析数百万个 XML 文件 - Java

c# - Linq to 实体插入 EF 1

.net - 使用 .NET 安全存储身份验证 token

windows-7 - 设置多个环境变量