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

x
?
Solved

Generate Hierarchical XML from SQL Server 2008

Posted on 2010-11-17
4
Medium Priority
?
757 Views
Last Modified: 2012-06-22
Hello experts,

I am attempting to write a SQL script that will generate an XML File from Users and profile table.

I want the structure to be as follows:::

<User ApplicationID="GUID1" UserId="GUID2" UserName="Test">
  <Profile UserId="GUID2" FirstName="" LastName="" LastUpdatedDate="" Gender="M" Occupation="" PersonalWebsite="" Address="" AptNumber="" City="" State="" PostalCode="" Country="" DayPhone="" DayPhoneExt="" EveningPhone="" EveningPhoneExt="" CellPhone="" Fax="" Company="" Address2="" City2="" State2="" PostalCode2="" Phone2="" Fax2="" Website2="editing" Newsletter="Html"/>
</User>

Open in new window


I also want the userid to be relational.

Here is my current script:::
USE [Database]
GO

SELECT ApplicationID AS 'User/@ApplicationID', UserID AS 'User/@UserId', UserName AS 'User/@UserName',
(SELECT UserId AS 'Profile/@UserId', FirstName AS 'Profile/@FirstName', LastName AS 'Profile/@LastName', LastUpdatedDate AS 'Profile/@LastUpdatedDate', Gender AS 'Profile/@Gender', BirthDate AS 'Profile/@BirthDate', Occupation AS 'Profile/@Occupation', 
PersonalWebsite AS 'Profile/@PersonalWebsite', Address AS 'Profile/@Address', AptNumber AS 'Profile/@AptNumber', City AS 'Profile/@City', State AS 'Profile/@State', PostalCode AS 'Profile/@PostalCode', Country AS 'Profile/@Country',
DayPhone AS 'Profile/@DayPhone', DayPhoneExt AS 'Profile/@DayPhoneExt', EveningPhone AS 'Profile/@EveningPhone', EveningPhoneExt AS 'Profile/@EveningPhoneExt', CellPhone AS 'Profile/@CellPhone', Fax AS 'Profile/@Fax', Company AS 'Profile/@Company', 
Address2 AS 'Profile/@Address2', City2 AS 'Profile/@City2', State2 AS 'Profile/@State2', PostalCode2 AS 'Profile/@PostalCode2', Phone2 AS 'Profile/@Phone2', Fax2 AS 'Profile/@Fax2', Website2 AS 'Profile/@Website2', Culture AS 'Profile/@Culture', 
Newsletter AS 'Profile/@Newsletter' FROM dbo.aspnet_CustomProfile FOR XML PATH('Profiles'),TYPE) 
FROM dbo.aspnet_Users
FOR XML PATH(''),
ROOT('Users')
GO

Open in new window

0
Comment
Question by:XGIS
[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
  • 2
  • 2
4 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 1000 total points
ID: 34157775

SELECT ApplicationID AS '@ApplicationID', UserID AS '@UserId', UserName AS '@UserName',
(
	SELECT UserId AS 'Profile/@UserId', FirstName AS 'Profile/@FirstName',
	LastName AS 'Profile/@LastName', LastUpdatedDate AS 'Profile/@LastUpdatedDate',
	Gender AS 'Profile/@Gender', BirthDate AS 'Profile/@BirthDate',
	Occupation AS 'Profile/@Occupation', PersonalWebsite AS 'Profile/@PersonalWebsite',
	Address AS 'Profile/@Address', AptNumber AS 'Profile/@AptNumber', City AS 'Profile/@City',
	State AS 'Profile/@State', PostalCode AS 'Profile/@PostalCode', Country AS 'Profile/@Country',
	DayPhone AS 'Profile/@DayPhone', DayPhoneExt AS 'Profile/@DayPhoneExt',
	EveningPhone AS 'Profile/@EveningPhone', EveningPhoneExt AS 'Profile/@EveningPhoneExt',
	CellPhone AS 'Profile/@CellPhone', Fax AS 'Profile/@Fax', Company AS 'Profile/@Company', 
	Address2 AS 'Profile/@Address2', City2 AS 'Profile/@City2', State2 AS 'Profile/@State2',
	PostalCode2 AS 'Profile/@PostalCode2', Phone2 AS 'Profile/@Phone2', Fax2 AS 'Profile/@Fax2',
	Website2 AS 'Profile/@Website2', Culture AS 'Profile/@Culture', 
	Newsletter AS 'Profile/@Newsletter'
	FROM dbo.aspnet_CustomProfile
	WHERE UserId = dbo.aspnet_Users.UserId
	FOR XML PATH(''),type,elements) 
FROM dbo.aspnet_Users
FOR XML PATH('User')
GO

Open in new window

0
 
LVL 7

Author Comment

by:XGIS
ID: 34159991
wow who would have thouht I was missing ,elements
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34159994
And a correlating condition and some extra levels.. but who's counting :)
0
 
LVL 7

Author Closing Comment

by:XGIS
ID: 34160624
Thanks very much a very quick and precise response
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

688 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