Solved

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

Posted on 2011-02-20
1
1,275 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:mitchellsjohnson
1 Comment
 
LVL 10

Accepted Solution

by:
John Claes earned 500 total points
Comment Utility
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

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now