无法将XML反序列化为List <t> </t>

时间:2015-01-03 12:38:59

标签: c# xml serialization collections

这是我正在尝试反序列化的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<EntityType>
  <Name>SomeName</Name>
  <Components>
    <ComponentAssembly>Some assembly name</ComponentAssembly>
  </Components>
</EntityType>

以下是我用来反序列化的数据合同:

using System.Collections.Generic;
using System.Runtime.Serialization;

namespace GameUtilities.Entities.DataContracts
{
[DataContract(Name="EntityType",Namespace="")]
public class EntityTypeData
{
    [DataMember(IsRequired=true,Order = 0)]
    public string Name { get; private set; }

    [DataMember(IsRequired=false,Order=1)]
    public List<ComponentEntry> Components { get; private set; }

    public EntityTypeData(string name, List<ComponentEntry> components = null)
    {
        Name = name;
        if(components == null)
        {
            Components = new List<ComponentEntry>();
        }
        else
        {
            Components = components;
        }
    }
}

[DataContract]
public class ComponentEntry
{
    [DataMember(IsRequired = true, Order = 0)]
    public string ComponentAssembly { get; private set; }

    public ComponentEntry(string componentAssembly)
    {
        ComponentAssembly = componentAssembly;
    }
}
}

反序列化它可以正常工作,但无论我在标签中放置了多少次,组件列表始终为空。我已经尝试将组件的[DataMemeber]属性标记为“IsRequired = true”,并且反序列化仍然完成而没有错误,但List未填充。你能看到我的数据合同有什么问题会导致失败吗?

编辑:作为测试,我使用上面的数据合同通过序列化程序运行了一个对象,以查看XML被吐出的内容。这就是我所看到的:

<EntityType xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Name>TESTNAME</Name>
    <Components xmlns:a="http://schemas.datacontract.org/2004/07/GameUtilities.Entities.DataContracts">
        <a:ComponentEntry>
            <a:ComponentAssembly>ONE</a:ComponentAssembly>
        </a:ComponentEntry>
        <a:ComponentEntry>
            <a:ComponentAssembly>TWO</a:ComponentAssembly>
        </a:ComponentEntry>
        <a:ComponentEntry>
            <a:ComponentAssembly>THREE</a:ComponentAssembly>
        </a:ComponentEntry>
   </Components>

这是我使用的序列化代码:

public static void SerializeObject<T>(string path, T obj)
    {
        FileStream fs = new FileStream(path,FileMode.Create);
        XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
        DataContractSerializer ser = new DataContractSerializer(typeof(T));

        //Serialize the data to a file
        ser.WriteObject(writer, obj);
        writer.Close();
        fs.Close();
    }

如您所见,为列出的每个ComponentAssembly创建了一个单独的ComponentEntry。有没有办法摆脱它,只是得到ComponentAssembly?

1 个答案:

答案 0 :(得分:0)

最简单的解决方案是定义收集数据合同。在使用ComponentEntry时,我认为没有办法让DataContractSerializer作为单独的类型。

[DataContract(Name="EntityType",Namespace="")]
public class EntityTypeData
{
    [DataMember(IsRequired=true,Order=0)]
    public string Name { get; private set; }

    [DataMember(IsRequired=false,Order=1)]
    public ComponentList Components { get; private set; }

    public EntityTypeData(string name, IEnumerable<string> components = null)
    {
        Name = name;
        Components = new ComponentList();
        if(components != null)
        {
            Components.AddRange(components);
        }
    }
}

[CollectionDataContract(ItemName = "ComponentAssembly", Namespace="")]
public class ComponentList : List<string> {}
var ser = new DataContractSerializer(typeof(EntityTypeData));
var entity = (EntityTypeData) ser.ReadObject(stream);