I have some xml in the form:
<Test>
<Parent Value1="1" Value2="2">
<Child value3="x"/>
</Parent>
<Parent Value1="1" Value2="2">
<Child value3="y"/>
</Parent>
<Parent Value1="a" Value2="b">
<Child value3="z"/>
</Parent>
</Test>
I am trying to use xelements and linq to group the child nodes under parent nodes that have the same attribute values. E.g.:
<Test>
<Parent Value1="1" Value2="2">
<Child value3="x"/>
<Child value3="y"/>
</Parent>
<Parent Value1="a" Value2="b">
<Child value3="z"/>
</Parent>
</Test>
To date, this is what I've got:
Dim l_xeXML As XElement = <Test>
<Parent Value1="1" Value2="2">
<Child value3=""/>
</Parent>
<Parent Value1="1" Value2="2">
<Child value3=""/>
</Parent>
<Parent Value1="a" Value2="b">
<Child value3=""/>
</Parent>
</Test>
Dim l_xeGROUPED As XElement = <Test>
<%= From l_xeExample As XElement In l_xeXML...<Parent>
Group l_xeExample By Key = New With {Key l_xeExample.@Value1, Key l_xeExample.@Value2} Into Group
Select l_GROUPED = New With {.Value1 = Key.Value1, .Value2 = Key.Value2}
Select <Parent Value1=<%= l_GROUPED.Value1 %> Value2=<%= l_GROUPED.Value2 %>>
<!-- Can't work out what to put here -->
</Parent> %>
</Test>
Debug.Print(l_xeGROUPED.ToString)
On the line <!-- Can't work out what to put here -->
, I have tried putting <%= l_xeExample.<Child> %>
however I get an error saying that l_xeExample is not within scope.
Can anybody help?
Thanks in advance
This will produce the desired output :
.......
.......
Dim l_xeGROUPED As XElement =
<Test>
<%= From l_xeExample As XElement In l_xeXML...<Parent>
Group l_xeExample By Key = New With {Key l_xeExample.@Value1, Key l_xeExample.@Value2}
Into Parent = Group
Select <Parent Value1=<%= Key.Value1 %> Value2=<%= Key.Value2 %>>
<%= From g As XElement In Parent.<Child>
Select g
%>
</Parent> %>
</Test>
Debug.Print(l_xeGROUPED.ToString)