The stored procedure
ALTER PROC doXml
as
select
1 as tag ,
ParentId as parent ,
CONVERT(VARCHAR(10), Id) + '-' AS [menuItem!1!X],
[text] as [menuItem!1!text] ,
[description] as [menuItem!1!description] ,
menuPath as [menuItem!1!path],
null as [menuItem!2!text] ,
null as [menuItem!2!description],
null as [menuItem!2!path]
from menuDB
where ParentId Is Null
union all
select
2 ,
m1.ParentId ,
CONVERT(VARCHAR(10), m2.ParentId) + '-' + CONVERT(VARCHAR(10), m2.Id) AS [menuItem!1!X],
m1.[text] ,
m1.[description] ,
m1.menuPath,
m2.[text] ,
m2.description,
m2.menuPath
from menuDB m1 , menuDB m2
where m2.parentid = m1.Id
ORDER BY 3
for xml explicit
GO
EXEC dbo.doXml
gave
<menuItem X="1-" text="Text A" description="Root A" path="A" />
<menuItem text="Text A1" description="Branch A1" path="A1" />
<menuItem text="Text A2" description="Branch A2" path="A2" />
<menuItem X="2-" text="Text B" description="Root B" path="B" />
<menuItem text="Text B1" description="Branch B1" path="B1" />
<menuItem text="Text B2" description="Branch B2" path="B2" />
This is at least ordered better
It might be best to an explicit OrderBy column to order children within parent
Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.