Linq to XML query to combine xml attributes

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am new to LINQ and Im trying to combine xml node attributes using a nested group by query.
I am having issues with getting the nesting of grouped data to work. I am trying to consolidate nodes with the same attribute key on each level.
I am not able to use the sItemGroup for the next level of nesting, the error is "sItemGroup does not exist in the current context.




/***** Here is the code ******/
XElement directory =
new XElement("NewSubj",
from subj in clincalData.Descendants("SubjectData")
//from subjdata in subj.Elements("ClinicalData").Elements("SubjectData")
group subj by (string)subj.Attribute("SubjectKey") into subjGroup
let subjKey = subjGroup.Key
select
new XElement("SubjectName",
new XAttribute("SubjectKey",
subjKey),
new XAttribute("numberOfSubjects",
subjGroup.Count()),
//get next study event group
from subj in subjGroup
from sItem in subjGroup.Descendants("StudyEventData")
group sItem by (string)sItem.Attribute("StudyEventOID") into sItemGroup
let sItemKey = sItemGroup.Key
select
new XElement("StudyEventData",
new XAttribute("StudyEventOID",sItemKey)),
//get next form item to group
from subj in subjGroup
from fItem in sItemGroup.Descendants("FormData")
group fItem by (string)fItem.Attribute("FormOID") into fItemGroup
let fItemKey = fItemGroup.Key
select
new XElement("FormData",
new XAttribute("FormOID", fItemKey)),
//get next form item to group
from igItem in fItemGroup.Descendants("ItemGroupData")
group igItem by (string)igItem.Attribute("ItemGroupOID") into igItemGroup
let igItemKey = igItemGroup.Key
select
new XElement("FormData",
new XAttribute("FormOID", igItemKey))));
Console.WriteLine(directory);



/********** Here is the xml ********/

<ClinicalData StudyOID="test
<SubjectData SubjectKey="901-klc
<SiteRef LocationOID="333 </SiteRef>
<StudyEventData StudyEventOID="Common
<FormData FormOID="SCR
<ItemGroupData ItemGroupOID="SCR_S1" TransactionType="Insert
<ItemData ItemOID="SCRNID" Value="900 </ItemData>
</ItemGroupData>
</FormData>
<FormData FormOID="SCR
<ItemGroupData ItemGroupOID="SCR_S1" TransactionType="Insert
<ItemData ItemOID="SCRNIDENT" Value="klc </ItemData>
</ItemGroupData>
</FormData>
<FormData FormOID="SCR
<ItemGroupData ItemGroupOID="SCR_S1" TransactionType="Insert
<ItemData ItemOID="SCRNDATE" Value="11/28/2010 </ItemData>
</ItemGroupData>
</FormData>
</StudyEventData>
</SubjectData>
<SubjectData SubjectKey="901-klc
<SiteRef LocationOID="333 </SiteRef>
<StudyEventData StudyEventOID="Common1
<FormData FormOID="SCR2
<ItemGroupData ItemGroupOID="SCR_S2" TransactionType="Insert
<ItemData ItemOID="SCRNID" Value="901 </ItemData>
</ItemGroupData>
</FormData>
<FormData FormOID="SCR2
<ItemGroupData ItemGroupOID="SCR_S2" TransactionType="Insert
<ItemData ItemOID="SCRNIDENT" Value="klc </ItemData>
</ItemGroupData>
</FormData>
<FormData FormOID="SCR2
<ItemGroupData ItemGroupOID="SCR_S2" TransactionType="Insert
<ItemData ItemOID="SCRNDATE" Value="11/28/2010 </ItemData>
</ItemGroupData>
</FormData>
</StudyEventData>
</SubjectData>
</ClinicalData>


Thanks for you help,
Karen

View the full article
 
Back
Top