对象反序列化XML类型转换错误

时间:2015-08-25 16:37:22

标签: c# .net xml serialization xml-deserialization

我收到错误 无法隐式转换类型'对象' to' LookUpCodeCollection'。存在显式转换(您是否错过了演员?) 当我在下面的这行代码中调用方法时

 obj = Deserialize(xml.ToString(), typeof(LookUpCodeCollection));

这是LookUpCode对象

    [System.Serializable()]
    public class LookUpCode
    {

        #region "Member Variables"


        private int mCodeID;
        private int? mCategoryID;

        private string mLookUpDescription;
        #endregion

        #region "Properties"
        public int CodeID
        {
            get { return mCodeID; }
            set { mCodeID = value; }
        }

        public int? CategoryID
        {
            get { return mCategoryID; }
            set { mCategoryID = value; }
        }
        public string LookUpDescription
        {
            get { return mLookUpDescription; }
            set { mLookUpDescription = value; }
        }
        #endregion

    }
}

LookUpCodeCollection

using System;
using System.Collections.Generic;
[System.Serializable()]
public class LookUpCodeCollection : System.Collections.ObjectModel.Collection<LookUpCode>, IList<LookUpCode>
{
}

XML

- <ArrayOfLookUpCode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <LookUpCode>
  <CodeID>1</CodeID> 
  <CategoryID>1</CategoryID> 
  <LookUpDescription>States</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>2</CodeID> 
  <CategoryID>2</CategoryID> 
  <LookUpDescription>Connecticut Cities by Zipcode</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>3</CodeID> 
  <CategoryID>3</CategoryID> 
  <LookUpDescription>Zipcode Connecticut Cities</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>4</CodeID> 
  <CategoryID>4</CategoryID> 
  <LookUpDescription>ContactType</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>5</CodeID> 
  <CategoryID>5</CategoryID> 
  <LookUpDescription>PhoneType</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>6</CodeID> 
  <CategoryID>6</CategoryID> 
  <LookUpDescription>DocumentGenerationType</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>7</CodeID> 
  <CategoryID>7</CategoryID> 
  <LookUpDescription>AmendmentType</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>8</CodeID> 
  <CategoryID>8</CategoryID> 
  <LookUpDescription>ContractorsTown_TownCodes</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>9</CodeID> 
  <CategoryID>9</CategoryID> 
  <LookUpDescription>Status</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>10</CodeID> 
  <CategoryID>10</CategoryID> 
  <LookUpDescription>Upload Document Types</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>11</CodeID> 
  <CategoryID>11</CategoryID> 
  <LookUpDescription>ExpirationWarning</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>12</CodeID> 
  <CategoryID>12</CategoryID> 
  <LookUpDescription>RenewalInterval</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>13</CodeID> 
  <CategoryID>13</CategoryID> 
  <LookUpDescription>Reports</LookUpDescription> 
  </LookUpCode>
- <LookUpCode>
  <CodeID>14</CodeID> 
  <CategoryID>14</CategoryID> 
  <LookUpDescription>ActiveInactive</LookUpDescription> 
  </LookUpCode>
  </ArrayOfLookUpCode>

反序列化方法

        //Declare the variables
        LookUpCodeCollection obj = new LookUpCodeCollection();

obj = Deserialize(xml.ToString(), typeof(LookUpCodeCollection));

反序列化方法

        private static object Deserialize(string input, Type toType)
    {
        System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(toType);

        using (StringReader sr = new StringReader(input))
            return ser.Deserialize(sr);
    }

2 个答案:

答案 0 :(得分:3)

Deserialize()会返回object。它将是适当的类型,但由于C#是强类型语言(并且没有从objectLookUpCodeCollection的隐式转换),编译器不知道如何将结果分配给LookUpCodeCollection类型的变量。正如错误所暗示的那样,您需要明确地投射它:

LookUpCodeCollection obj = (LookUpCodeCollection)Deserialize(xml.ToString(), typeof(LookUpCodeCollection));

或者,您可以将Deserialize方法设为通用:

private static T Deserialize<T>(string input)
{
    System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));

    using (StringReader sr = new StringReader(input))
        return (T)ser.Deserialize(sr);
}

答案 1 :(得分:1)

您需要再次将反序列化的结果转换为LookUpCodeCollection

(LookUpCodeCollection)ser.Deserialize(sr);

虽然obj对象的类型正确,但Deserialize方法的返回类型为object,因此不会将其转换为所需的结果类型。您可以将反序列化方法的返回类型更改为LookUpCodeCollection,并且反序列化应该有效(无需进一步更改代码):

private static LookUpCodeCollection Deserialize(string input, Type toType)
{
    System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(toType);

    using (StringReader sr = new StringReader(input))
    {
        return (LookUpCodeCollection)ser.Deserialize(sr);
    }
}

为了更灵活,常用方法(在本例中为Deserialize)经常使用the ultimate base class of all classes in the .NET Framework - Object,因此在这种情况下您需要始终强制转换为派生类型(在您的情况下为{{ 1}})。

如果要重新使用不同数据类型的反序列化函数,请查看C# Generics