gpt4 book ai didi

c# - 此 XmlWriter 不支持 base64 编码数据

转载 作者:数据小太阳 更新时间:2023-10-29 01:57:45 24 4
gpt4 key购买 nike

我有这样一个类:

public class Data
{
public string Name { get; set; }
public int Size { get; set; }
public string Value { get; set; }

[NonSerialized] public byte[] Bytes;
}

List<Data>打下面的序列化方法,偶尔死掉

InvalidOperationException "This XmlWriter does not support base64 encoded data."

如你所见,我没有直接编码任何东西,只是使用默认的序列化机制。

private static XDocument Serialize<T>( T source )
{
var target = new XDocument( );
var s = new XmlSerializer( typeof( T ) );
using( XmlWriter writer = target.CreateWriter( ) )
{
s.Serialize( writer, source );
}
return target;
}

数据将有Name属性是用下划线分隔的英文单词。 Value除了添加数学运算符或数字(它们是数学表达式)外,属性将类似。

有谁知道是什么原因造成的,我该如何纠正?

最佳答案

使用 [XmlIgnore] 而不是 [NonSerialized]。根据 MSDN,后者用于 SOAP 和二进制格式化程序。 :

When using the BinaryFormatter or SoapFormatter classes to serialize an object, use the NonSerializedAttribute attribute to prevent a field from being serialized. For example, you can use this attribute to prevent the serialization of sensitive data.

The target objects for the NonSerializedAttribute attribute are public and private fields of a serializable class. By default, classes are not serializable unless they are marked with SerializableAttribute. During the serialization process all the public and private fields of a class are serialized by default. Fields marked with NonSerializedAttribute are excluded during serialization. If you are using the XmlSerializer class to serialize an object, use the XmlIgnoreAttribute class to get the same functionality.

请注意,我很惊讶您的原始代码甚至可以编译 - 当我尝试时,它说 [NonSerialized] 只能应用于字段...

关于c# - 此 XmlWriter 不支持 base64 编码数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3480724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com