DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Shallow and Deep Copies in JavaScript: What’s the Difference?
  • What Is Ant, Really?
  • Security Is a Platform Property, Not a Pipeline Step
  • An XGBoost Property Valuation Postmortem: Leakage, Overfitting, and SHAP Surprises

Trending

  • Building an AI Incident Response Runbook: What Engineering Teams Should Do in the First 24 Hours
  • Improving Repeated Analytics Workloads With Databricks Disk Cache
  • RavenDB Launches Quill to Bring Production AI Agents to Enterprise SQL Systems, No Migration Required
  • How to Perform Response Verification in REST-Assured Java for API Testing: Part 2
  1. DZone
  2. Coding
  3. Languages
  4. Using the DataContractSerializer to Serialize and Deserialize

Using the DataContractSerializer to Serialize and Deserialize

By 
Merrick Chaffer user avatar
Merrick Chaffer
·
Dec. 14, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
26.1K Views

Join the DZone community and get the full member experience.

Join For Free

To serialize a Business object marked with [DataContract] and properties marked with [DataMember] attributes…

public static string DataContractSerializeObject<T>(T objectToSerialize)

        {

using (var output = new StringWriter())

            {

using (var writer = new XmlTextWriter(output) { Formatting = Formatting.Indented })

                {

var dataContractSerializer = new DataContractSerializer(typeof(T), EntityUtilities.GetAllKnownTypes(), int.MaxValue, true, true, null);

                    dataContractSerializer.WriteObject(writer, objectToSerialize);

return output.GetStringBuilder().ToString();

                }

            }

        }

Then to deserialize back to your DataContract type, use this logic…

public static T Deserialize<T>(string xml)

        {

using (Stream stream = new MemoryStream())

            {

byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);

                stream.Write(data, 0, data.Length);

                stream.Position = 0;

var dataContractSerializer = new DataContractSerializer(typeof(T), EntityUtilities.GetAllKnownTypes(), int.MaxValue, true, true, null);

return (T)dataContractSerializer.ReadObject(stream);

            }

        }
Property (programming) Object (computer science)

Published at DZone with permission of Merrick Chaffer. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Shallow and Deep Copies in JavaScript: What’s the Difference?
  • What Is Ant, Really?
  • Security Is a Platform Property, Not a Pipeline Step
  • An XGBoost Property Valuation Postmortem: Leakage, Overfitting, and SHAP Surprises

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook