I was recently tasked with some ETL related activity.

The objective was to get data from a table to a hierarchical XML file.

This is how I went about it.

1> Get the XSD File.
2> Map the elements in the XSD with our present data.
3> Use XSD tool to create a class file.
4> Use Visual Studio to include this class in a project.
5> Create an instance of the class and then write the XML.

I have simplified the code by excluding the DB connection and hardcoding the values.

Code:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="RECORDSET" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="RECORDSET" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="STUDENT_RECORD">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="firstname" type="xs:string" minOccurs="0" />
              <xs:element name="lastname" type="xs:string" minOccurs="0" />
              <xs:element name="dob" type="xs:string" minOccurs="0" />
              <xs:element name="subjectlist" minOccurs="0" maxOccurs="1">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="subject" type="xs:string" nillable="true" minOccurs="0" maxOccurs="unbounded" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
2> Open the visual studio command line window and type
PHP Code:
xsd //l:cs /n:StudentXSD student.xsd 
You'll be able to get a C Sharp file.
3> Add reference to this C Sharp file in the Visual Studio Environment.

4> The following code will help you create a XML file based on this structure.
c# Code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Xml.Serialization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using StudentXSD;
  11. using System.Windows.Forms;
  12.  
  13. namespace StudentXSD
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.  
  22.         private void cmdWriteXML_Click(object sender, EventArgs e)
  23.         {
  24.             RECORDSETSTUDENT_RECORD rc1 = new RECORDSETSTUDENT_RECORD();
  25.             string[] subjects = { "math", "science", "nuclear physics"};
  26.  
  27.             rc1.firstname = "Adam";
  28.             rc1.lastname = "Cross";
  29.             rc1.dob = DateTime.Today.ToShortDateString();
  30.             rc1.subjectlist = subjects;
  31.             SerializeToXML(rc1);
  32.         }
  33.         static public void SerializeToXML(RECORDSETSTUDENT_RECORD studRec)
  34.         {
  35.             XmlSerializer serializer = new XmlSerializer(typeof(RECORDSETSTUDENT_RECORD));
  36.             TextWriter textWriter = new StreamWriter(@"D:\User\studentData.xml");
  37.             serializer.Serialize(textWriter, studRec);
  38.             textWriter.Close();
  39.         }
  40.     }
  41. }