In this post,I am going to show you how DataContract export DataContract into xsd ,and then how an object is converted into xml.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; using System.Xml.Schema; using System.Xml; namespace DataContract_Demo { [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; } } class Program { static void Main(string[] args) { GenerateXsd(); GenerateXML(); } 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); } } 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" }); } } }



No comments:
Post a Comment