Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-20
1
Medium Priority
?
1,565 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 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

705 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