“此 XmlReader 不支持 ReadElementContentAsBase64 方法”
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace XmlTest
{
class TestClass : IXmlSerializable
{
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
var data = new byte[3];
reader.ReadStartElement();
reader.ReadElementContentAsBase64(data, 0, data.Length);
}
public void WriteXml(XmlWriter writer)
{
var data = new byte[] { 1, 2, 3 };
writer.WriteBase64(data, 0, data.Length);
}
public static void Main()
{
var serializer = new DataContractSerializer(typeof(TestClass));
var stringWriter = new StringWriter();
using (var writer = XmlWriter.Create(stringWriter))
{
serializer.WriteObject(writer, new TestClass());
}
var stringReader = new StringReader(stringWriter.ToString());
using (var reader = XmlReader.Create(stringReader))
{
serializer.ReadObject(reader, true);
}
}
}
}
ReadElementContentAsBase64
行抛出 NotSupportedException
并显示消息:
此 XmlReader 不支持 ReadElementContentAsBase64 方法。使用 CanReadBinaryContent 属性来查明阅读器是否实现它。
(我检查过,CanReadBinaryContent 返回 true)
我正在使用 Microsoft .NET 3.5 框架实现。
什么可能导致这种情况?
注意:我有意将 DataContractSerializer 与 IXmlSerialized 混合在一起。我意识到 DataContractSerializer 更常见的方法是将我的类设为 [DataContract]。
编辑: 我现在正在使用一种解决方法:Convert.FromBase64String(reader.ReadElementContentAsString())
不过,我想知道为什么常规方法会失败。
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace XmlTest
{
class TestClass : IXmlSerializable
{
public XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
var data = new byte[3];
reader.ReadStartElement();
reader.ReadElementContentAsBase64(data, 0, data.Length);
}
public void WriteXml(XmlWriter writer)
{
var data = new byte[] { 1, 2, 3 };
writer.WriteBase64(data, 0, data.Length);
}
public static void Main()
{
var serializer = new DataContractSerializer(typeof(TestClass));
var stringWriter = new StringWriter();
using (var writer = XmlWriter.Create(stringWriter))
{
serializer.WriteObject(writer, new TestClass());
}
var stringReader = new StringReader(stringWriter.ToString());
using (var reader = XmlReader.Create(stringReader))
{
serializer.ReadObject(reader, true);
}
}
}
}
The ReadElementContentAsBase64
line throws NotSupportedException
with message:
ReadElementContentAsBase64 method is not supported on this XmlReader. Use CanReadBinaryContent property to find out if a reader implements it.
(I checked, and CanReadBinaryContent returns true)
I'm using the Microsoft .NET 3.5 framework implementation.
What could possibly cause this?
Note: I'm intentionally mixing DataContractSerializer with IXmlSerializable. I realize that the more common approach for DataContractSerializer is to make my class a [DataContract].
Edit: I'm now using a workaround:Convert.FromBase64String(reader.ReadElementContentAsString())
Still, I wonder why the regular way fails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也遇到了这个问题。 linq 的 doc.CreateReader() 创建的 XmlReader 没有实现 Base64 解码。我通过首先保存到 MemoryStream 并从中创建 XmlReader 来解决这个问题:
I also encountered this problem. The XmlReader created by linq's
doc.CreateReader()
does not implement Base64 decoding. I got around it by first saving to a MemoryStream and creating an XmlReader from that:我接受了罗伯特的答案并将其变成了扩展方法,享受吧!
I took Robert's answer and turned it into an Extension Method, enjoy!