Adding Functionality

Because of the extensibility that is LinqtoXML, DDI Survey Reader contains all namespaces currently known for DDI3 standards. Using the following basic pieces of code you can return any element in the document.

 

Finding the Root Element

Before you can start using the document correctly, you find the root element by:

//Load the XML Document.
XDocument SurveyXMLDocument = XDocument.Load(Filelocation);
//Find the Root Element.
XElement RootElement = SurveyXMLDocument.Root;

Extract Specific Element:

Now you’ve gotten the root you can find any element by referencing it. E.g

RootElement.Element(s + "StudyUnit").Element(s + "Purpose").Element(r + "Content").Value

This will return the Content’s Value. <r:Content>This is it’s value!</r:Content>

RootElement.Element(s + "StudyUnit").Element(s + "Purpose").Element(r + "Content").Name

Will return the elements name.

 

Extract a Group of Elements:

If you want more than just 1 element, you can return several using .Descendants()

e.g

To return all Elements under the <r:Citation> tag.

foreach (XElement CitationElement in RootElement.Descendants(s + "StudyUnit").Descendants(r + "Citation").Descendants())
{

    CitationElement.Name.ToString()
    CitationElement.Value

    CitationItemCount++;
}

Performing a Query:

Using the following code you can query elements and attributes as you see fit.

YourVariable will be compared to the id=” “ Attribute of the "<l:CodeScheme> Element.

 

var CodeSchemeQuery = from CodeSchemeID in RootElement.Descendants(l + "LogicalProduct").Elements(l + "CodeScheme").Attributes("id")
                                                    where CodeSchemeID.Value == YourVariable
                                                   select CodeSchemeID;

 

//foreach CodeSchemeID found the QuestionItem
foreach (XAttribute CodeSchemeQueryResult in CodeSchemeQuery)
     {

               //Do Stuff

     }

Last edited Apr 27, 2011 at 2:06 PM by lucasmoffitt, version 2

Comments

No comments yet.