Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6124465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:08:10+00:00 2026-05-23T16:08:10+00:00

I am using the DataContractSerializer in .NET 3.5 to deserialize xml. The xml was

  • 0

I am using the DataContractSerializer in .NET 3.5 to deserialize xml. The xml was previously serialized from a group of related entities in an entity model, backed by the entity framework 3.5. There are many references, and the xml extensively contains all the values of the members and keys of each referenced entity.

The top level entity deserializes fine, but the referenced entities do not.

This is the code I’m using to serialize and deserialize:

    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;
    }

This is a snippet of the XML:

<?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>

I’ve been stuck on this for a couple days and I’ve exhausted all search results that I could find. Using this technique can clearly avoid hand writing tons of code for each entity type in our model, of which there are 143.

So to reiterate, the top level entity deserializes fine, but the referenced entities do not. So Asset is loaded and Asset.AssetCategory (among many more) resolves to null after deserialization, and I need help to fix it so all references get instantiated. Please, anyone?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:08:10+00:00Added an answer on May 23, 2026 at 4:08 pm

    Sometimes you need to tell the serializer about other “Known Types”.

    See the MSDN documentation:

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

    You can do this through the config, through attributes, or through parameters/a property on DataContractSerializer.

    There is a full example the documentation links to here:

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using DataContractSerializer to serialize/deserialize my classes to/from XML. Everything works fine, but at
How do I remove XML namespaces from an object's XML representation serialized using DataContractSerializer?
I am creating serialized XML for a LINQ to SQL project using the DataContractSerializer
I'm using ASP.NET Web API with Entity Framework. I changed my default serializer to
I have an ASP.NET 4 web app that is using the Entity Framework to
I have a simple ADO.NET Entity Framework 4.0 model (edmx) which defines database tables
I've been using WCF's DataContract and DataContractSerializer to read/write objects to XML files. We
I am attempting to retrieve a list of objects from Entity Framework via WCF,
I am using .NET 3.5SP1 and DataContractSerializer to serialize a class. In SP1, they
I'm serializing my DTO to XML messages using DataContractSerializer, however I need to support

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.