简体   繁体   English

Xml序列化错误:无法转换类型的对象

[英]Xml Serialization Error: Unable to cast object of type

Can some one help for a strange Serialization issue which I getting in a one environment and it working fine in all environments..but it failed a particular environment..so I specified as strange here 有人可以解决一个奇怪的序列化问题吗?我在一个环境中遇到问题,并且在所有环境中都能正常工作..但是在特定环境下无法通过..所以我在这里指定为“奇怪”

Code

public SomeType[] Deserialize(string xml)
{
    XmlSerializer serializer = new XmlSerializer(typeof(SomeType[]));
    StringReader stringReader = new StringReader(xml);
    SomeType[] types = (SomeType[])serializer.Deserialize(stringReader);
    stringReader.Close();
    return types;
}

Serialized XML data: 序列化的XML数据:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSomeType>
  <SomeType>
    <Field1>val</Field1>
    <Field2>val</Field2>
    <Field3>val</Field3>
  </SomeType>
</ArrayOfSomeType>

And the exception is: 唯一的例外是:

System.InvalidCastException: Unable to cast object of type 'SomeType[]' to type 'SomeType'. System.InvalidCastException:无法将类型为“ SomeType []”的对象转换为类型为“ SomeType”。

at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSomeType.Write11_SomeType(Object o) 在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterSomeType.Write11_SomeType(对象o)

The error here is in the Serialize method that you don't show. 这里的错误在于您未显示的Serialize方法中。 Your Deserialize method works without error. 您的Deserialize方法可以正常工作。 The following Serialize method works fine: 下面的Serialize方法可以正常工作:

static string Serialize(SomeType[] values)
{
    using (var sw = new StringWriter())
    {
        var serializer = new XmlSerializer(typeof(SomeType[]));
        serializer.Serialize(sw, values);
        return sw.ToString();
    }
}

If I had to guess, you have the following: 如果我不得不猜测,您将拥有以下优势:

var serializer = new XmlSerializer(typeof(SomeType));

If you want the exact same output without unnecessary namespace alias declarations, change the method to include: 如果要获得完全相同的输出而没有不必要的名称空间别名声明,请将方法更改为包括:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("","");
serializer.Serialize(sw, values, ns);

Edit showing the current code working fine: 编辑显示当前代码工作正常:

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

public class SomeType
{
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    public string Field3 { get; set; }
}
public class MyTest
{
    public static SomeType[] Deserialize(string xml)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(SomeType[]));
        StringReader stringReader = new StringReader(xml);
        SomeType[] types = (SomeType[])serializer.Deserialize(stringReader);
        stringReader.Close();
        return types;
    }
    public static void Main()
    {
        string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<ArrayOfSomeType>
   <SomeType>
      <Field1>val</Field1>
      <Field2>val</Field2>
      <Field3>val</Field3>
   </SomeType>
</ArrayOfSomeType>";
        var items = Deserialize(xml);
        foreach (var item in items)
        {
            Console.WriteLine("{0}, {1}, {2}",
                item.Field1, item.Field2, item.Field3);
        }
    }
}

暂无
暂无

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

相关问题 无法投放对象类型错误 - Unable to cast object Type Error 无法将“MongoDB.Bson.Serialization.Serializers.DateTimeSerializer”类型的对象转换为“MongoDB.Bson.Serialization.IBsonSerializer”类型 - Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.DateTimeSerializer' to type 'MongoDB.Bson.Serialization.IBsonSerializer` Linq强制转换Xelement错误:无法将类型为'System.Xml.Linq.XElement'的对象强制转换为'System.IConvertible' - Linq cast conversion Xelement error: Unable to cast object of type 'System.Xml.Linq.XElement' to type 'System.IConvertible' 错误消息“无法转换类型的对象” - Error message “Unable to cast object of type” 对象反序列化XML类型转换错误 - Object Deserialization XML Type Cast Error 无法转换类型的对象 - Unable to cast object of type 无法将对象...转换为类型 - Unable to cast object … to type 无法转换类型的对象 - Unable to cast object of type “无法将&#39;System.Net.Http.Formatting.JsonContractResolver&#39;类型的对象强制转换为&#39;Newtonsoft.Json.Serialization.DefaultContractResolver&#39;。” - “Unable to cast object of type 'System.Net.Http.Formatting.JsonContractResolver' to type 'Newtonsoft.Json.Serialization.DefaultContractResolver'.” 无法将类型为“Newtonsoft.Json.Linq.JObject”的对象强制转换为“System.Runtime.Serialization.ISafeSerializationData” - Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'System.Runtime.Serialization.ISafeSerializationData'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM