How do I export DataGrid to XML when I need to format the structure ?

I have the basic like this
public void ConnectionXML()
        {
            SqlConnection con = new SqlConnection("Data Source=MYServer;Initial Catalog=MyDB;Integrated Security=True");

            con.Open();

            SqlCommand cmd = new SqlCommand("SELECT * FROM Warehouse", con);

            cmd.CommandType = CommandType.Text;

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet("LocationEnvelope");

            da.Fill(ds, "Warehouse");

            dataGridView1.DataSource = ds.Tables["Warehouse"];

            //dataGridView1.DataBind();

            StreamWriter xmlDoc = new StreamWriter("Joplindo.xml");
            ds.WriteXml(xmlDoc);
            xmlDoc.Close();

Open in new window


The result is:
<LocationEnvelope>
  <Warehouse>
    <id>3</id>
    <Branch>Joplin</Branch>
    <Manager>Bilbo Baggins</Manager>
    <Phone>xxx-xxx-xxx</Phone>
    <Email>BilboBaggins@company.com</Email>
    <Day>Monday</Day>
    <IN>2013-09-20T08:00:00.00-05:00</IN>
    <OUT>2013-09-20T14:30:00.00-05:00</OUT>
  </Warehouse>
</LocationEnvelope>

Open in new window


I need to format it to look like this:
<LocationEnvelope>
  <Warehouse Key ="3">
    <Branch>Joplin</Branch>
    <Manager>Bilbo Baggins</Manager>
    <Phone>xxx-xxx-xxx</Phone>
    <Email>BilboBaggins@company.com</Email>
    <Schedule>
        <Day>Monday</Day>
        <IN>2013-09-20T08:00:00.00-05:00</IN>
        <OUT>2013-09-20T16:30:00.00-05:00</OUT>
    </Schedule>
  </Warehouse>
</LocationEnvelope>

Open in new window


Can anyone tell me how to define the schema inside the code to do this. I have a schema built I just don't know how to use it in the project to do the formatting.
GD_GRAYAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
You don't have control over the XML output from DataSet.WriteXml.  If you need a custom format, then you could define a DTO that you can serialize to the XML that you need.  The data transfer object could be automapped to the DataTable, as described here:

Extending Datatables with AutoMapper
http://www.motimendelovich.com/2012/04/extending-datatables-with-automapper/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.