SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Creating XML from SQL Server 2005 - What are my options?


Creating XML from SQL Server 2005 - What are my options?

Author
Message
ucbus 92278
ucbus 92278
SSC-Addicted
SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)

Group: General Forum Members
Points: 444 Visits: 119
Hello:
I am new to SQl server. Here is my question and hope some one can help me.

We have a database containingg the following structure: GrandParent (GP),parent(p1,p2,p3), child tables.
My task is to create an XML out of the tables taking given a record in GP, it should give me the whole hierarchy of all tables underneath it "like a tree structure" if you would call it.

1. I have an XSD how my XML should look like. Can I register this XSD with the SQL server and ask it to generate a XML confirming to the XSD I have?
The database is having very deep hierarchy and I appreciate your help.
My plan is to use VB.net with minimal coding on the client side. please let me know.
Mauve
Mauve
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10540 Visits: 2071
ucbus 92278 (10/18/2009)
Hello:
I am new to SQl server. Here is my question and hope some one can help me.

We have a database containingg the following structure: GrandParent (GP),parent(p1,p2,p3), child tables.
My task is to create an XML out of the tables taking given a record in GP, it should give me the whole hierarchy of all tables underneath it "like a tree structure" if you would call it.


1. I have an XSD how my XML should look like. Can I register this XSD with the SQL server and ask it to generate a XML confirming to the XSD I have?

Yes, you can register it. No, you can't just tell SQL Server to create the XML based upon the schema. Your SQL query is doing all of the work.

My plan is to use VB.net with minimal coding on the client side. please let me know.

Some process will still have to do the work. Either SQL Server via a complex query creating XML or the middle-tier (client) taking simple result sets and then transforming it into XML. Although you can create complex hierarchical XML from SQL Server, the latter may be more "efficient".



In addition to the documentation in the Books Online (BOL), and Jacob's nice tutorials, here are links to a number of Microsoft Technical articles that describe how SQL Server 2005 supports XML:

XML Support in Microsoft SQL Server 2005
http://msdn.microsoft.com/en-us/library/ms345117(SQL.90).aspx

XML Options in Microsoft SQL Server 2005
http://msdn.microsoft.com/en-us/library/ms345110(SQL.90).aspx

What's New in FOR XML in Microsoft SQL Server 2005
http://msdn.microsoft.com/en-us/library/ms345137(SQL.90).aspx

XML Indexes in SQL Server 2005
http://msdn.microsoft.com/en-us/library/ms345121(SQL.90).aspx

XML Best Practices for Microsoft SQL Server 2005
http://msdn.microsoft.com/en-us/library/ms345115(SQL.90).aspx


(PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.
ucbus 92278
ucbus 92278
SSC-Addicted
SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)

Group: General Forum Members
Points: 444 Visits: 119
Mauve:
Thanks for the response.
Just to eloborate on my requirement. My XML/XSD would follow the following format
-------------
<message>
<message header>
<messsage ID>
<date time >
< etc etc>
</message header>
<message body>
<grand parent>
<parent1>
<child1>
<child 2>
</parent1>
<parent2>
<child1>
<child 2>
</parent2>
</grand parent>
</message body>
</message>

---------------------
Now, I have created the same structure in SQL Server tables and loaded the data. Now my task is to get the data from the tables ( which exactly match my XML tags) in the XML format

I have tried to use "FOR XML" tag by creating a view of the data, However this resulted in multiple rows in a FLAT structure. To get the XML in the format I needed, I would have to use XSLT transformation I guess.

Second option is to use "XML Views". I find MS documents on using XDRs to create XML Views. However I am not sure how to create XDRs given that I have XSD and matching relational database structure. Can any one help with the approach. Now how to get the data tree specific to say a particular record, in my case a <message>?

Third, Object Relational Mapping is something that could be helpful. I am not sure what can be used in case of .NET and VB.net. ?


Please let me know what is the ideal approach given the above scenario.
Mauve
Mauve
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10540 Visits: 2071
In our use of XML as a data parsing mechanism from/to our .Net layer and SQL Server 2005 we are using traditional relational structures (tables) to hold the data. The stored procedures take the input XML and selectively extract, using XQuery syntax, data values from it for DML operations against the tables. I.e., it is only used to simplify the data parameters to the stored procedures.

Similarly, we construct a single XML document from the relational structure, using FOR XML PATH, to return to the .Net layer vs. numerous result sets (i.e., a .Net DataSet) which the .Net layer would have to iterate through. See (one of) Jacob Sebastian's articles for some sample syntax: http://www.sqlservercentral.com/articles/XML/62289/

We are not, as you are proposing, attempting to use SQL Server 2005 as an object relational database utilizing XML and Annotated XSD Schemas. Therefore, I cannot offer any further assistance in this area.


(PHB) I think we should build an SQL database. (Dilbert) What color do you want that database? (PHB) I think mauve has the most RAM.
Brandie Tarvin
Brandie Tarvin
SSC Guru
SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)

Group: General Forum Members
Points: 160479 Visits: 9869
[b]ucbus 92278 (10/20/2009)I have tried to use "FOR XML" tag by creating a view of the data, However this resulted in multiple rows in a FLAT structure. To get the XML in the format I needed, I would have to use XSLT transformation I guess.


Not necessarily. You're making a couple of assumptions here. Just because the XML looks flat doesn't mean that the XSD file won't read it. I have XML files like that which I import into SQL Server using SSIS with an XSD file all the time. It works just fine.

Also, there are three different versions of the FOR XML command. Are you sure you tried all three of them to get the exact format you wanted? (aside from the flat issue, that is).

If you don't want to use SSMS for your results, create an SSRS report and a subscription to that report that exports into an XML format. I've done that quite often and it works amazingly well. Even if it looks "flat", an XSD file will read it. I know this for a fact.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
jcrawf02
jcrawf02
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21852 Visits: 19324
For those of us who don't know enough yet, what exactly do you mean by the XML file looking "flat"? (anyone)

Thanks,

Jon

---------------------------------------------------------
How best to post your question
How to post performance problems
Tally Table:What it is and how it replaces a loop

"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."
Brandie Tarvin
Brandie Tarvin
SSC Guru
SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)

Group: General Forum Members
Points: 160479 Visits: 9869
I'm assuming "flat" means that the XML is all on one line with no pretty carriage returns between each data element. Sort of like:

<column1>2adfad</column1><column2>adfeoue</column2><column3>etouen</column3>

If that's not what the OP means by the term, then please correct me.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
ucbus 92278
ucbus 92278
SSC-Addicted
SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)

Group: General Forum Members
Points: 444 Visits: 119
When I said "flat" I meant from the "relational structure" point of view. For example you have a Parent and two children, then you can represent this by
parent1, child1
parent1, child 2

or parent1,child1,child2

anyways, I have resolved it by using "for xml path" and using "Select sub-queris".
Brandie Tarvin
Brandie Tarvin
SSC Guru
SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)SSC Guru (160K reputation)

Group: General Forum Members
Points: 160479 Visits: 9869
Could you post some sample code for the resolution so that people who need this in the future can better understand your solution?

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
ucbus 92278
ucbus 92278
SSC-Addicted
SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)SSC-Addicted (444 reputation)

Group: General Forum Members
Points: 444 Visits: 119
Sure. I have posted the sample code at http://www.sqlservercentral.com/Forums/Topic827863-21-1.aspx. I could as well post it here, but why to waste storage?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search