What about this trick?
DECLARE @tbl TABLE(ID INT,ParentID INT,Type VARCHAR(10),Value VARCHAR(10))
INSERT INTO @tbl VALUES
(1,0,'Num','327')
,(2,1,'Sector','21')
,(3,1,'Sector','22')
,(4,2,'Product','ARF')
,(5,2,'Product','BRF')
,(6,3,'Product','NS')
,(7,3,'Product','MS')
,(8,4,'Quantity','20')
,(9,5,'Quantity','30')
,(10,6,'Quantity','25')
,(11,7,'Quantity','60');
WITH recCTE AS
(
SELECT *,0 AS HLevel,Type + N'="' + CAST(Value AS NVARCHAR(MAX)) + N'" ' AS attr
FROM @tbl WHERE ParentID=0
UNION ALL
SELECT t.*,r.HLevel+1,attr+t.Type + N'="' + CAST(t.Value AS NVARCHAR(MAX)) + N'" '
FROM @tbl AS t
INNER JOIN recCTE AS r ON t.ParentID=r.ID
)
SELECT CAST(N'<row ' + attr + N'/>' AS XML)
FROM recCTE
WHERE HLevel=3
FOR XML PATH(''),ROOT('root')
The result
<root>
<row Num="327" Sector="22" Product="MS" Quantity="60" />
<row Num="327" Sector="22" Product="NS" Quantity="25" />
<row Num="327" Sector="21" Product="BRF" Quantity="30" />
<row Num="327" Sector="21" Product="ARF" Quantity="20" />
</root>
This XML is easy to query... The deepest Level (here I took HLevel=3) can be found generically - but you'd need to provide more details...
UPDATE
The following will not use a given depth as filter but a query to check, if a node is a leaf-node
I added one more row at the end
DECLARE @tbl TABLE(ID INT,ParentID INT,Type VARCHAR(100),Value VARCHAR(100))
INSERT INTO @tbl VALUES
(1,0,'Num','327')
,(2,1,'Sector','21')
,(3,1,'Sector','22')
,(4,2,'Product','ARF')
,(5,2,'Product','BRF')
,(6,3,'Product','NS')
,(7,3,'Product','MS')
,(8,4,'Quantity','20')
,(9,5,'Quantity','30')
,(10,6,'Quantity','25')
,(11,7,'Quantity','60')
,(13,11,'SomeMore','Test as fourth');
WITH recCTE AS
(
SELECT t.*
,0 AS HLevel
,t.Type + N'="' + CAST(t.Value AS NVARCHAR(MAX)) + N'" ' AS attr
,CASE WHEN EXISTS(SELECT 1 FROM @tbl AS x WHERE x.ParentID=t.ID) THEN 0 ELSE 1 END AS IsLeaf
FROM @tbl AS t WHERE ParentID=0
UNION ALL
SELECT t.*
,r.HLevel+1
,attr+t.Type + N'="' + CAST(t.Value AS NVARCHAR(MAX)) + N'" '
,CASE WHEN EXISTS(SELECT 1 FROM @tbl AS x WHERE x.ParentID=t.ID) THEN 0 ELSE 1 END AS IsLeaf
FROM @tbl AS t
INNER JOIN recCTE AS r ON t.ParentID=r.ID
)
SELECT CAST(N'<row ' + attr + N'/>' AS XML)
FROM recCTE
WHERE IsLeaf=1
FOR XML PATH(''),ROOT('root')
The result
<root>
<row Num="327" Sector="22" Product="MS" Quantity="60" SomeMore="Test as fourth" />
<row Num="327" Sector="22" Product="NS" Quantity="25" />
<row Num="327" Sector="21" Product="BRF" Quantity="30" />
<row Num="327" Sector="21" Product="ARF" Quantity="20" />
</root>
UPDATE 2: Using your real data
As you've found out alread, your question was quite a mess... Don't know what you really need, but if I run your real data through this query I'd get this:
DECLARE @tbl TABLE(ID INT,ParentID INT,Type VARCHAR(100),Value VARCHAR(100))
INSERT INTO @tbl VALUES
(38,0,'Num','327')
,(39,38,'Sector','21')
,(40,38,'Sector','22')
,(43,40,'Product','NS')
,(44,40,'Product','MS')
,(50,40,'Temp','RAS')
,(48,44,'Quantity','60')
,(47,43,'Quantity','25')
,(41,39,'Product','ARF')
,(42,39,'Product','BRF')
,(49,39,'Temp','RAS')
,(51,39,'Cible','Acarien A.')
,(46,42,'Quantity','30')
,(52,42,'Cible','Acarien B.')
,(45,41,'Quantity','20');
WITH recCTE AS
(
SELECT t.*
,0 AS HLevel
,t.Type + N'="' + CAST(t.Value AS NVARCHAR(MAX)) + N'" ' AS attr
,CASE WHEN EXISTS(SELECT 1 FROM @tbl AS x WHERE x.ParentID=t.ID) THEN 0 ELSE 1 END AS IsLeaf
FROM @tbl AS t WHERE ParentID=0
UNION ALL
SELECT t.*
,r.HLevel+1
,attr+t.Type + N'="' + CAST(t.Value AS NVARCHAR(MAX)) + N'" '
,CASE WHEN EXISTS(SELECT 1 FROM @tbl AS x WHERE x.ParentID=t.ID) THEN 0 ELSE 1 END AS IsLeaf
FROM @tbl AS t
INNER JOIN recCTE AS r ON t.ParentID=r.ID
)
SELECT CAST(N'<row ' + attr + N'/>' AS XML)
FROM recCTE
WHERE IsLeaf=1
FOR XML PATH(''),ROOT('root')
The result
<root>
<row Num="327" Sector="22" Temp="RAS" />
<row Num="327" Sector="22" Product="MS" Quantity="60" />
<row Num="327" Sector="22" Product="NS" Quantity="25" />
<row Num="327" Sector="21" Temp="RAS" />
<row Num="327" Sector="21" Cible="Acarien A." />
<row Num="327" Sector="21" Product="BRF" Quantity="30" />
<row Num="327" Sector="21" Product="BRF" Cible="Acarien B." />
<row Num="327" Sector="21" Product="ARF" Quantity="20" />
</root>