Question

I'm using Microsoft.WindowsAzure.Storage v2.1.0.3.

Trying serialize TableContinuationToken to XML.

Serialization code is:

String tokenXml;
var serializer = new XmlSerializer(typeof(TableContinuationToken));
using (var writer = new StringWriter())
{
  var writerSettings = new XmlWriterSettings { OmitXmlDeclaration = true, NewLineChars = String.Empty };
  using (var xmlWriter = XmlWriter.Create(writer, writerSettings))
  {
    serializer.Serialize(xmlWriter, token);
  }
  tokenXml = writer.ToString();
}

Deserialization code is:

TableContinuationToken token; 
var serializer = new XmlSerializer(typeof(TableContinuationToken));
using (var stringReader = new StringReader(tokenXml))
{
  token = (TableContinuationToken)serializer.Deserialize(stringReader);
}

Very simple. But when i trying deserialize token following exception occure:

System.InvalidOperationException : There is an error in XML document (1, 26).
----> System.Xml.XmlException : Unexpected Element 'ContinuationToken'

After removing tags from serialized token code works fine!

May be it's BUG in Microsoft.WindowsAzure.Storage v2.1.0.3 ? Or i'am doing something wrong?

Thanks.

Was it helpful?

Solution

I just came across this issue. Instead of using your own XmlSerializer instance, you have to use the WriteXml() and ReadXml() methods on a TableContinuationToken instance.

    public static string SerializeToken(TableContinuationToken token)
    {
        string serialized = null;
        if (token != null)
        {
            using (var writer = new StringWriter())
            {
                using (var xmlWriter = XmlWriter.Create(writer))
                {
                    token.WriteXml(xmlWriter);
                }
                serialized = writer.ToString();
            }
        }

        return serialized;
    }

    public static TableContinuationToken DeserializeToken(string token)
    {
        TableContinuationToken contToken = null;
        if (!string.IsNullOrWhiteSpace(token))
        {
            using (var stringReader = new StringReader(token))
            {
                contToken = new TableContinuationToken();
                using (var xmlReader = XmlReader.Create(stringReader))
                {
                    contToken.ReadXml(xmlReader);
                }
            }
        }

        return contToken;
    }

I assume this is because of the serialization attributes on the class and properties, but I have not verified that. I only know that the provided code is the solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top