"ReadElementContentAsBase64 method is not supported on this XmlReader"
Asked Answered
C

2

9
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.

Conscience answered 22/9, 2010 at 0:47 Comment(1)
Did you eventually solve this problem? I have exactly the same issue (CanReadBinaryContent is true, but it still fails), but additionally, I do not have access to the serialiser that actually crashes (I embed an object that implements IXmlSerializable in a DataContract).Bealle
J
9

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
     }
Justify answered 22/3, 2011 at 8:53 Comment(0)
G
7

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);
    }
}
Gasket answered 22/2, 2013 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.