How DataContract serialize CLR object into XML

In this post, we will delve into the concepts of DataContract serialization and XML conversion in .NET. We’ll start by exploring how to export a DataContract into an XSD (XML Schema Definition), followed by a demonstration of how an object is converted into XML using the DataContractSerializer.

Prerequisites

Before we dive in, make sure you have a basic understanding of C# and the .NET framework. We’ll be working with DataContract attributes, serialization, and XML generation.

DataContract and DataMember Attributes

The DataContract and DataMember attributes play a crucial role in controlling how objects are serialized and deserialized in .NET. The DataContract attribute marks a class as serializable, and the DataMember attribute specifies which properties are included in the serialization process.

Here’s a sample class, Person, annotated with these attributes:

using System.Runtime.Serialization;

[DataContract(Namespace = "http://aspdotnetcodebook.blogspot.com", Name = "Person")]
public class Person
{
    [DataMember(Name = "FirstName")]
    public string FirstName { get; set; }

    [DataMember(Name = "LastName")]
    public string LastName { get; set; }
}

Exporting DataContract to XSD

First, let’s explore how to export a DataContract into an XSD. This can be useful for sharing your data structure with other systems or tools.

using System;
using System.Xml.Schema;
using System.Xml;

namespace DataContract_Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            GenerateXsd();
        }

        private static void GenerateXsd()
        {
            XsdDataContractExporter exporter = new XsdDataContractExporter();
            exporter.Export(typeof(Person));

            foreach (XmlSchema xml in exporter.Schemas.Schemas("http://aspdotnetcodebook.blogspot.com"))
            {
                var writer = new XmlTextWriter(Console.Out) { Formatting = Formatting.Indented };
                xml.Write(writer);
            }
        }
    }
}

The GenerateXsd method utilizes the XsdDataContractExporter to export the Person class into an XSD schema. The generated schema can be useful for documentation and integration purposes.

Serializing an Object to XML

Next, let’s see how to serialize an object into XML using the DataContractSerializer.

using System;
using System.Runtime.Serialization;
using System.Xml;

namespace DataContract_Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            GenerateXML();
        }

        private static void GenerateXML()
        {
            var Objwriter = new XmlTextWriter(Console.Out) { Formatting = Formatting.Indented };
            var ser = new DataContractSerializer(typeof(Person));

            ser.WriteObject(Objwriter, new Person() { FirstName = "X", LastName = "Y" });
        }
    }
}

The GenerateXML method uses the DataContractSerializer to serialize a Person object into XML. The resulting XML will reflect the structure of the Person class and include the provided values.

Conclusion

In this post, we’ve explored the fundamental concepts of DataContract serialization and XML conversion in .NET. We learned how to annotate a class with DataContract and DataMember attributes to control serialization behavior. Additionally, we saw how to export a DataContract to an XSD schema and how to use the DataContractSerializer to convert an object into XML.

Serialization and XML conversion are powerful techniques that enable data interchange and integration between systems. By understanding these concepts, you’re better equipped to work with data in various formats across different platforms.

Please do not post any spam link in the comment box😊

إرسال تعليق (0)
أحدث أقدم