TechyMedia

A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer

NAVIGATION - SEARCH

XML Serialization and Deserialization using XSD -C#,VB.NET

Convert xml to xsdXml serialization in .NET can be done in various ways.but the easiest one which I believe is using xsd.exe to create the corresponding model dataset class from the xml file.Step by step procedure explained.xml to xsd conversion

Xml serialization in .NET can be done in various ways.but the easiest one which I believe is using xsd.exe to create the corresponding  model  class file from the xml  file.Follow the easy steps explained below you can have your serializer-Deserializer ready immediately.

1) Launch Visual studio command prompt

2) Run the command  as  xsd.exe   D:\Book.xml /outputdir:D:\Test

Give your xml file path instead of  D:\Book.xml  .The xsd file will be created in the directory D:\Test , by this command.


3) Run command  xsd.exe  /c /l:CS  D:\Book.xsd /outputdir:D:\Test

After successful execution of this  command you will get your C# model class Book.cs.See the directory directory D:\Test for the Book.cs file.


4) This generated model data set class can be used for serializing and deserializing the xml  data.

De-serialization Code Snippet    

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Book));

       using (StreamReader reader = new StreamReader("Book.xml"))

       {

          Book book= (Book)xmlSerializer.Deserialize(reader);

       }

 

Serialization Code Snippet

      

using (StreamWriter writer = new StreamWriter("Book.xml"))

       {

           Book book= null;

           xmlSerializer.Serialize(writer, book);

       } 

 

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading