1

I have to use an externally given xml structure (huge). I use the xsd tool of visual studio to generate classes that should be (de)serialized using xmlserializer. Since we switched from VS2010 to VS2012 (but still targeting .NET 4.0), I have problems deserializing the XML. I broke it down to the following code:

using System.IO;
using System.Xml;
using System.Xml.Serialization;

using Microsoft.VisualStudio.TestTools.UnitTesting;


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute]
[System.Diagnostics.DebuggerStepThroughAttribute]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlRootAttribute("DecoderParameter", Namespace = "", IsNullable = false)]
public class DecoderParameterType
{
    private string[] decoderUpdatePointsField;

    /// <remarks/>
    [XmlAttributeAttribute(DataType = "integer")]
    public string[] DecoderUpdatePoints
    {
        get
        {
            return this.decoderUpdatePointsField;
        }
        set
        {
            this.decoderUpdatePointsField = value;
        }
    }
}

[TestClass]
public class UnitTest1
{
    #region Public Methods and Operators

    [TestMethod]
    public void TestMethod1()
    {
        var fileName = "c:\\temp\\test.xml";

        var deserializer = new XmlSerializer(typeof(DecoderParameterType));

        var output = new DecoderParameterType { DecoderUpdatePoints = new[] { "5", "7", "9" } };

        using (var fs = new FileStream(fileName, FileMode.Create))
        {
            deserializer.Serialize(fs, output);
        }

        using (var sr = new XmlTextReader(fileName))
        {
            var myParameter = (DecoderParameterType)deserializer.Deserialize(sr);
        }
    }

    #endregion
}

This code snippet fails with an Exception:

System.Xml.XmlException: 'None' is an invalid XmlNodeType.

It works if I remove "DataType = integer" from the XmlAttributeAttribute.

Now I have the following questions:

  • Why does installing .NET4.5 change the behaviour of a .NET4.0 - program? Or is this not the case and I am missing something? (Before I installed VS2012, this worked fine! Now it is neither in VS2010 nor in VS2012 working)
  • What side effects has the removal of the datatype declaration?
  • Which other datatype declarations are also affected? I have a lot of these declarations in the generated code, not only integer (nonNegativeInteger, date, etc...).

Update: The problem occurs only if the variable is an array.

Kind regards

Maroni
  • 31
  • 3
  • 1
    I had a similar error occur to me once, but the problem wasn't the .NET version. The XML itself had one of its nested items removed, and a ReadEndElement() would read the root end tag prematurely. I would first check the integrity of the XML content itself. – OnoSendai May 02 '13 at 14:44
  • I checked this; the XML seems to be fine. I created a small program just doing the above. If I start it on a machine having only .NET 4.0, it works. If it has 4.5, it fails. – Maroni May 02 '13 at 16:09
  • I found something that appears similar. Read through this and see if it helps. http://stackoverflow.com/questions/14689305/serialization-breaks-in-net-4-5 – Dave May 02 '13 at 16:27
  • @Dave: Thank you, this was very helpful; the configuraiton entry did the trick. – Maroni May 02 '13 at 17:22

1 Answers1

0

Well, the first bullet is the easy one:

  • Why does installing .NET4.5 change the behaviour of a .NET4.0 - program?

Because .NET 4.5 is an over-the-top install, not a side-by-side install. When you install .NET 4.5 you are changing the 4.0 assemblies. The act of targeting 4.0 vs 4.5 simply determines whether the IDE will let you reference 4.5 specific features. Even when targeting 4.0, once you install 4.5 you are using the 4.5 implementation with any code-changes (bug-fixes, changed behaviors, and new bugs) associated with 4.5.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900