Solved

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

Posted on 2011-02-20
1
1,462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

630 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