【问题标题】:DataContractSerializer not deserializing referencesDataContractSerializer 不反序列化引用
【发布时间】:2011-09-29 03:59:37
【问题描述】:

我正在使用 .NET 3.5 中的 DataContractSerializer 来反序列化 xml。 xml 之前是从实体模型中的一组相关实体序列化的,由实体框架 3.5 提供支持。引用很多,xml广泛包含每个引用实体的成员和键的所有值。

顶级实体反序列化很好,但引用的实体没有。

这是我用来序列化和反序列化的代码:

    public static T DCDeserializeXml<T>(string xml)
    {
        MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(xml));
        using (
        XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.Unicode,
                   new XmlDictionaryReaderQuotas(), null))
        {
            DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T), null, Int32.MaxValue, false, true, null);
            return (T)dataContractSerializer.ReadObject(reader, true);
        }
    }

    public static string DCSerializeToXml<T>(T obj)
    {
        DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T), null, Int32.MaxValue, false, true, null);

        String text;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            dataContractSerializer.WriteObject(memoryStream, obj);                
            byte[] data = new byte[memoryStream.Length];
            Array.Copy(memoryStream.GetBuffer(), data, data.Length);
            text = Encoding.UTF8.GetString(data);
        }
        return text;
    }

这是 XML 的 sn-p:

<?xml version="1.0" encoding="utf-8"?>
<Assets>
    <Asset z:Id="i1" xmlns="http://schemas.datacontract.org/2004/07/XLayer" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
        <EntityKey z:Id="i2" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data">
            <a:EntityContainerName>XModelContainer</a:EntityContainerName>
            <a:EntityKeyValues>
                <a:EntityKeyMember>
                    <a:Key>AssetGUID</a:Key>
                    <a:Value i:type="z:guid">7424f615-43db-4834-b15a-5befa46bfd55</a:Value>
                    </a:EntityKeyMember></a:EntityKeyValues>
                    <a:EntitySetName>AssetSet</a:EntitySetName>
                    </EntityKey>
                    <AssetGUID>7424f615-43db-4834-b15a-5befa46bfd55</AssetGUID>
                    <Created>2011-06-23T13:34:12.893</Created>
                    <Description/>
                    <npAudioInfoReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
                        <a:EntityKey i:nil="true" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data"/>
                    </npAudioInfoReference>
                    <npCampaigns/>
                    <npCategory z:Id="i3">
                        <EntityKey z:Id="i4" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data">
                            <a:EntityContainerName>XModelContainer</a:EntityContainerName>
                            <a:EntityKeyValues>
                                <a:EntityKeyMember>
                                    <a:Key>CategoryID</a:Key>
                                    <a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1</a:Value>
                                </a:EntityKeyMember>
                            </a:EntityKeyValues>
                            <a:EntitySetName>AssetCategorySet</a:EntitySetName>
                        </EntityKey>
                        <AM_DataDocumentTypes/>
                        <CategoryID>1</CategoryID>
                        <CategoryName>Generic Content</CategoryName>
                        <npAssets>

我已经被困在这个问题上几天了,我已经用尽了所有能找到的搜索结果。使用这种技术可以明显避免为我们模型中的每种实体类型手写大量代码,其中有 143 个。

因此,重申一下,顶级实体可以很好地反序列化,但引用的实体不会。所以 Asset 被加载并且 Asset.AssetCategory (以及更多)在反序列化后解析为 null,我需要帮助来修复它,以便所有引用都被实例化。请问有人吗?

【问题讨论】:

    标签: c# .net entity-framework datacontractserializer


    【解决方案1】:

    有时您需要告诉序列化程序有关其他“已知类型”的信息。

    请参阅 MSDN 文档:

    http://msdn.microsoft.com/en-us/library/ms730167.aspx

    您可以通过配置、属性或 DataContractSerializer 上的参数/属性来执行此操作。

    这里有一个完整的文档链接示例:

    http://msdn.microsoft.com/en-us/library/ms751512.aspx

    【讨论】:

    • 这看起来很有希望。我会在早上试一试。非常感谢。
    猜你喜欢
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2013-06-01
    • 1970-01-01
    相关资源
    最近更新 更多