Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ReadElementContentAsBase64 method is not supported on this XmlReader"

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.

like image 729
Stefan Monov Avatar asked Sep 22 '10 00:09

Stefan Monov


2 Answers

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
     }
like image 124
Robert Jeppesen Avatar answered Jan 29 '23 17:01

Robert Jeppesen


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);
    }
}
like image 29
jhilden Avatar answered Jan 29 '23 16:01

jhilden