I am using DataContractSerializer in .NET 3.5 to deserialize xml. Xml was previously serialized from a group of related objects in an entity model supported by entity framework 3.5. There are many links, and xml extensively contains all the values of the elements and keys of each object referenced.
The top-level entity is deserialized by a fine, but reference objects do not.
This is the code I'm using for serialization and deserialization:
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 an XML fragment:
<?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 got stuck on this a couple of days and I ran out of all the search results I could find. Using this method, you can clearly avoid writing a ton of code for each type of entity in our model, of which 143.
, , , . , Asset , Asset.AssetCategory( ) null , , , . , -?