繁体   English   中英

无法使用C#xmlserializer反序列化先前序列化的XML

[英]Cannot deserialize previously serialized XML with c# xmlserializer

我必须使用外部给定的xml结构(巨大)。 我使用Visual Studio的xsd工具生成应使用xmlserializer进行(反)序列化的类。 由于我们从VS2010切换到VS2012(但仍以.NET 4.0为目标),因此在反序列化XML时遇到了问题。 我将其分解为以下代码:

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
}

此代码段失败,并带有异常:

System.Xml.XmlException:'None'是无效的XmlNodeType。

如果我从XmlAttributeAttribute中删除“ DataType =整数”,它将起作用。

现在我有以下问题:

  • 为什么安装.NET4.5会更改.NET4.0程序的行为? 还是不是这种情况,我想念什么? (在我安装VS2012之前,这工作正常!现在在VS2010和VS2012中都无法正常工作)
  • 删除数据类型声明有哪些副作用?
  • 其他哪些数据类型声明也受到影响? 我在生成的代码中有很多这样的声明,不仅是整数(nonNegativeInteger,date等)。

更新 :仅当变量是数组时,才会出现问题。

亲切的问候

好吧,第一个项目符号很简单:

  • 为什么安装.NET4.5会更改.NET4.0程序的行为?

因为.NET 4.5是一个顶层安装,而不是并行安装。 安装.NET 4.5时,您将更改4.0程序集 针对4.0 vs 4.5的行为仅决定了IDE是否允许您引用4.5的特定功能。 即使以4.0为目标,安装4.5后,您也将4.5实施与4.5关联的任何代码更改(错误修复,更改的行为和新的错误)一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM