Tuesday, May 3, 2011

How to create C# data transfer object (DTO) class from a given XML?

  • Have your xml ready
  • On visual studio command prompt
 
        xsd <ur xml file path> - this will generate xsd
        xsd <ur just generated xsd file> /classes- this will generate cs
 
and finally use following sample code to load xml
 
StreamReader str = new StreamReader("cd_catalog.xml");
      XmlSerializer xSerializer = new XmlSerializer(typeof(CATALOG));
 
      CATALOG myCdCatalogue = (CATALOG)xSerializer.Deserialize(str);
 
      foreach (CATALOG.CDRow cd in myCdCatalogue.CD)
      {
        Console.WriteLine(cd.TITLE);
        Console.WriteLine(cd.ARTIST);
        Console.WriteLine(cd.COUNTRY);
        Console.WriteLine(cd.COMPANY);
        Console.WriteLine(cd.PRICE);
        Console.WriteLine(cd.YEAR);
        Console.WriteLine();
      }
      str.Close();
 
>>> xml to C#, C#, .net 3.5, XML
 
trivedimehulk@gmail.com
 
 

No comments:

Post a Comment