Solved

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

Posted on 2011-02-20
1
1,308 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
ID: 34940839
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

813 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