Create XML Output from XSD and Query in T-SQL or .NET

We have a requirement to generate an XML output from an XSD file (a schema) and from a group of relational database tables (SQL Server  2008 R2).  Ideally, we would be  able to run a query that joins multiple tables (say dbo.customer and dbo.order where order is joined to customer by customer_id) in SQL Server that returns XML that conforms to the XSD.
sample query that would run nightly:  
Select c.customer_id, c.customer_fname, c.customer_lname, o.order_id from dbo.customer c, dbo.order  o where c.customer_id = o.customer_id

Ideally, we could run a SQL job that  runs a query each night to generate this  XML.  If this  is not feasible, we could  run a ASP.NET console app that accomplishes the requirement.

We have evaluated Annotated XSD Schemas but have been unsuccessful  thus far and have  ead that these do not perform well.  If you have used these successfully and could provide a sample of how to generate the query above that would be appreciated.
http://msdn.microsoft.com/en-us/library/ms998283.aspx
mitchellsjohnsonAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
John ClaesConnect With a Mentor Senior .Net Consultant & Technical AnalistCommented:
I would suggest something like this


Want to map a DataSet containing a single table to XML?  The following code illustrates how to fork some information from an database into a table and write it to a XML file.

// DataSet to XML. 
private void SingleTableToXml()
{
    string connectionstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(@"data\northwind.mdb");

    OleDbConnection conn = new OleDbConnection(connectionstr);

    DataSet myDS = new DataSet("DataSetTransformed");

    string sql="SELECT CategoryID, CategoryName, Description FROM Categories";
    //Create a DataAdapter to load data from original data source to the DataSet
    OleDbDataAdapter myAdapter = new OleDbDataAdapter(sql, conn);

    myAdapter.Fill(myDS, "Categories");

    //disconnect the database
    conn.Close();

    //Write out the schema that this DataSet created, 
    //use the WriteXmlSchema method of the DataSet class
    myDS.WriteXmlSchema(Server.MapPath("categories.xsd"));
      

    // To write out the contents of the DataSet as XML, 
    //use a file name to call the WriteXml method of the DataSet class
    myDS.WriteXml(Server.MapPath("categoies.xml"), XmlWriteMode.IgnoreSchema);

}

Open in new window


Here's a segment of the output XML file:
Here's a segment of the output XML file:

Open in new window


As you can see, the root element of the XML file takes the name of the DataSet. In .NET 2.0, many of the tasks that could previously only be performed by a DataSet can also be tackled by a DataTable; such as XML transformation.

Source :
http://dotnetslackers.com/articles/ado_net/MappingDataSetToXMLAndBackwards.aspx

Part : DataSet with a single table to XML


regards

poor beggar
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.