0

Good day, I need some help, I have a dataset which I populate from my db, I also have a xsd Schema file I need to create an xml file using the data from the db and the xsd file.

Can anyone please help me.

All the best

4
  • Use following : DataSet ds = new DataSet(); ds.WriteXml("Filename", XmlWriteMode.WriteSchema); Commented Jul 1, 2015 at 10:04
  • I already have the schema file, i need to basically combine my db data and my schema to create the xml file Commented Jul 1, 2015 at 13:48
  • You don't need to write the schema, but I recommend comparing you existing schema against the generated schema. If the two schemas are the same, then just used the generated one. Makes code a lot simpler. Commented Jul 1, 2015 at 17:18
  • Do you possibly have a code example for me as I am very new to xml. Do my db column names have to be the same as my xml attriute names? Commented Jul 2, 2015 at 7:58

1 Answer 1

1

You can cross reference the database names with the datatable names in you Select Query by using "as" : "Select abc as xyz from table1". "abc" is the database name and xyz is the datatable name which will be the xml tag name.

Here is an example of writing a DataSet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Data;

namespace ConsoleApplication34
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable("MyTable");

            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Id", typeof(int));

            dt.Rows.Add(new object[] { "John", 1});
            dt.Rows.Add(new object[] { "Mary", 2});
            dt.Rows.Add(new object[] { "Dick", 3});
            dt.Rows.Add(new object[] { "Harry", 4});
            dt.Rows.Add(new object[] { "Jane", 5});

            DataSet ds = new DataSet("MySet");
            ds.Tables.Add(dt);

            ds.WriteXml(FILENAME, XmlWriteMode.WriteSchema);
            // or
            //ds.WriteXml(FILENAME);

        }

    }


}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.