“此 XmlReader 不支持 ReadElementContentAsBase64 方法”

发布于 2024-09-24 06:23:26 字数 1845 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月静好 2024-10-01 06:23:26

我也遇到了这个问题。 linq 的 doc.CreateReader() 创建的 XmlReader 没有实现 Base64 解码。我通过首先保存到 MemoryStream 并从中创建 XmlReader 来解决这个问题:

     Stream s = new MemoryStream();
     XmlSerializer serializer = new XmlSerializer(typeof(SerializableDocument));   
     document.Save(s);
     s.Seek(0, SeekOrigin.Begin);

     using (XmlReader newReader = XmlReader.Create(s))
     {
        SerializableDocument serializableDocument = (SerializableDocument)serializer.Deserialize(newReader);
        // do stuff with it
     }

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:

     Stream s = new MemoryStream();
     XmlSerializer serializer = new XmlSerializer(typeof(SerializableDocument));   
     document.Save(s);
     s.Seek(0, SeekOrigin.Begin);

     using (XmlReader newReader = XmlReader.Create(s))
     {
        SerializableDocument serializableDocument = (SerializableDocument)serializer.Deserialize(newReader);
        // do stuff with it
     }
梦过后 2024-10-01 06:23:26

我接受了罗伯特的答案并将其变成了扩展方法,享受吧!

public static T DeserializeWithBinaryData<T>(this XElement el)
{
    var xDoc = el.ToXmlDocument();
    using (var ms = new MemoryStream())
    {
        xDoc.Save(ms);
        ms.Seek(0, SeekOrigin.Begin);
        var serializer = new XmlSerializer(typeof (T));
        return (T)serializer.Deserialize(ms);
    }
}

I took Robert's answer and turned it into an Extension Method, enjoy!

public static T DeserializeWithBinaryData<T>(this XElement el)
{
    var xDoc = el.ToXmlDocument();
    using (var ms = new MemoryStream())
    {
        xDoc.Save(ms);
        ms.Seek(0, SeekOrigin.Begin);
        var serializer = new XmlSerializer(typeof (T));
        return (T)serializer.Deserialize(ms);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文