2012-11-07 90 views
0

我有一个应用程序需要序列化一个自定义对象并将其发送到一个Windows服务,自定义对象包含2个自定义对象列表和一个int,字符串字典。当我尝试序列化对象我收到错误消息:序列化列表/字典

There was an error generating the XML document.

我周围的一派,发现这通常是由于没有被设置为正确的序列化的数据类型之一。所以我已经通过并验证了所有自定义类的序列化,并且据我所知它已正确设置。

我现在的问题是,列表和字典默认情况下是可序列化的,还是需要做某些事情才能序列化它们?或者,是否有更好的方法来序列化可执行文件之间传递的自定义对象集合?

编辑:

主要自定义类:

[Serializable] 
class MoveInInfoRequest : ServerRequestData 
{ } 
[Serializable] 
[XmlInclude(typeof(GetUnitTypesResponseData)), XmlInclude(typeof(VendorObj.RequiredFields)), 
    XmlInclude(typeof(VendorObj.InsuranceChoice)), XmlInclude(typeof(VendorObj.ProrateSettings))] 
public class MoveInInfoResponse : ServerResponseData 
{ 
    public GetUnitTypesResponseData UnitTypesInfo 
    { get; set; } 
    public List<VendorObj.RequiredFields> RequiredFields 
    { get; set; } 
    public Dictionary<int, String> RentalPeriods 
    { get; set; } 
    public List<VendorObj.InsuranceChoice> InsCoverageAmounts 
    { get; set; } 
    public VendorObj.ProrateSettings ProrateOptions 
    { get; set; } 
} 

Sampple子类:其他两个类都建立一个类似这只是更长的时间,但他们只使用默认的数据类型。

<Serializable(), DataContract([Namespace]:="*companyNamespace*")> _ 
Public Class InsuranceChoice 
    Public Sub New() 
    End Sub 
    <DataMember()> _ 
    Public InsuranceChoiceID As Integer 
    <DataMember()> _ 
    Public CoverageDescription As String 
    <DataMember()> _ 
    Public Premium As Decimal 
    <DataMember()> _ 
    Public ActualCoverageAmount As Decimal 

End Class 
+3

张贴代码将有助于得到答案:) – Mihai

+0

他们是序列化的,是的。正如@Mihai所说,代码将会有所帮助。 – Yatrix

+0

列表和字典可以无需任何操作即可序列化。很明显,你正在把一些东西放在它们之中,或者把别的东西串起来。 – LightStriker

回答

1

这取决于你试图序列化他们。特别是,如果您使用的是XmlSerializer,则Dictionary对象不可序列化,但如果您使用的是DataContractSerializer,则它们是。你应该很好地序列化一个列表。

如果您想要替代Xml序列化,可以使用Json.Net序列化为JSON。

参考文献:

Serialize Class containing Dictionary member

Serializing .NET dictionary

Why doesn't XmlSerializer support Dictionary?

http://theburningmonk.com/2010/05/net-tips-xml-serialize-or-deserialize-dictionary-in-csharp/

+0

使用(?),因为我试图缩短rentalperiods对象而不是写一个新类。感谢他们的联系,让我更清楚为什么我遇到了麻烦。 –

1

这是一个相当普遍的问题,当涉及到序列化。

集合实施IDictionarycannot be serialized

你可以使用一个DataContractSerializer,但更好的解决方案(在我看来)是创建自己的字典类不从IDictionary

的例子继承这样的类可以发现here

一旦你在你的解决方案中实现了类,只要做到这一点:

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var response = new MoveInInfoResponse 
      { 
       RentalPeriods = new SerializableDictionary<int, string> 
       { { 1, "Period 1" }, { 2, "Period 2" } } 
      }; 

      string xml = Serialize(response); 
     } 

     static string Serialize(Object obj) 
     { 
      var serializer = new XmlSerializer(obj.GetType()); 
      var settings = new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }; 

      using (var stream = new StringWriter()) 
      { 
       using (var writer = XmlWriter.Create(stream, settings)) 
        serializer.Serialize(writer, obj); 
       return stream.ToString(); 
      } 
     } 
    } 

    [Serializable] 
    public class MoveInInfoResponse 
    { 
     public SerializableDictionary<int, String> RentalPeriods 
     { get; set; } 
    } 
} 

生成以下XML文件:

<MoveInInfoResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <RentalPeriods> 
    <Item> 
     <Key> 
     <int>1</int> 
     </Key> 
     <Value> 
     <string>Period 1</string> 
     </Value> 
    </Item> 
    <Item> 
     <Key> 
     <int>2</int> 
     </Key> 
     <Value> 
     <string>Period 2</string> 
     </Value> 
    </Item> 
    </RentalPeriods> 
</MoveInInfoResponse> 
+0

感谢您的工作。这可能在未来派上用场。对于这个实现来说,对于有问题的数据库来说,它更适合作为自己的类。 –