I have following example table
Code here
CREATE TABLE XMLData
(
NodeName NVARCHAR(255),
AttributA NVARCHAR(255),
AttributB NVARCHAR(255),
AttributC NVARCHAR(255),
)
INSERT INTO XMLData VALUES
('RowA','','abcd','efgh'),
('RowB','wxyz',NULL,NULL),
('RowC',NULL,'qwer','tyui'),
('RowD','stuv','erty','fghj')
SELECT * FROM dbo.XMLData
How can I get following XML ?
<NodeA>
<NodeB />
<NodeC AttributeX="">
<RowA AttributeA="" AttributeB="abcd" AttributeC="efgh" />
<RowB AttributeA="wxyz" />
<RowC AttributeB="qwer" AttributeC="tyui" />
<RowD AttributeA="stuv" AttributeB="erty" AttributeC="fghj" />
</NodeC>
</NodeA>
I am beginner with XML, but I tried to play with something like this
SELECT
(
SELECT
(
SELECT '' AS '@AttributeX' FOR XML PATH('NodeC'),TYPE
-- How to get table rows here ?
)
FOR XML PATH('NodeB'),TYPE -- Here it creates additional end NodeB tag
)
FOR XML PATH('NodeA'),TYPE

.nodes()...