Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Creating XML from SQL Server 2005 - What are my options? Expand / Collapse
Author
Message
Posted Sunday, October 18, 2009 10:26 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 15, 2011 3:36 PM
Points: 24, 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.
Post #804798
Posted Monday, October 19, 2009 7:56 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 24, 2014 11:03 AM
Points: 1,182, Visits: 1,970
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.
Post #805046
Posted Tuesday, October 20, 2009 7:09 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 15, 2011 3:36 PM
Points: 24, 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.
Post #805662
Posted Tuesday, October 20, 2009 8:31 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 24, 2014 11:03 AM
Points: 1,182, Visits: 1,970
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.
Post #805766
Posted Wednesday, October 21, 2009 4:11 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:48 AM
Points: 7,194, Visits: 6,338
[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 Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #806346
Posted Wednesday, October 21, 2009 6:41 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 6:02 AM
Points: 2,668, Visits: 19,229
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."
Post #806432
Posted Wednesday, October 21, 2009 6:50 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:48 AM
Points: 7,194, Visits: 6,338
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 Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #806443
Posted Thursday, December 3, 2009 8:49 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 15, 2011 3:36 PM
Points: 24, 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".
Post #828261
Posted Thursday, December 3, 2009 8:58 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:48 AM
Points: 7,194, Visits: 6,338
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 Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #828271
Posted Friday, December 4, 2009 8:11 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 15, 2011 3:36 PM
Points: 24, 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?
Post #828962
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse