XLinq Query Examples

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

In this pos I will show some intersting XLing examples used to query the data in the Visual Studio project file. Samples in this document are based on XML file shown in this post
and are all preceeded with following code, which loads the root element from XML file and set the namespace member to default namespace of document.

XElement solutionXml = XElement.Load(projectFile);
XNamespace m_Namespace = solutionXml.GetDefaultNamespace();

Sample I: Element with specific subelement

Retrieve all ItemGroup elements which contain at least one element Compile.

var compilingItemGroup =
    from element in solutionXml.Elements(m_Namespace + "ItemGroup")
   
where element.Elements(m_Namespace + "Compile").Count() > 0  
   
select element;

Sample II-a: Subelements with specific attribut value

Retrieve all Compile elements which contains Include Attribute with value "Hello.cs"

var compilingFiles =
    from  element in
solutionXml.Elements(m_Namespace + "ItemGroup").
          Elements(m_Namespace + "Compile")
    where element.Attribute("Include") != null &&
          element.Attribute("Include").Value.IndexOf("Hello.cs") > -1
    select element;

Sample II-b: Subelements with specific attribute value and
                        using of filter function

Retrieve all Compile elements which contains Include Attribute with value "Hello.cs", but with using of filter function.

private bool filter(XElement x, string val)
{
 
//Func<XElement, bool> f1 = x => filter;

  return
x.Attribute("Include") != null && 
         x.Attribute("Include").Value.IndexOf(val) > 1;
}

 var compilingFiles =
       from  element in 
            
solutionXml.Elements(m_Namespace + "ItemGroup").
             Elements(m_Namespace + "Compile")
       where filter(element, process)
       select element;

Sample III-a: Nested query

Retrieve all ItemGroup elements which contains Content elements with Include attribute set on value "Dummy.aspx".
This example demonstrates usage of nested queries.

var contentItemGroup = from element in
    solutionXml.Elements(m_Namespace + "ItemGroup
    where

      (from contentEl in element.Elements(m_Namespace + "Content")
         where contentEl.Attribute("Include") != null &&
         contentEl.Attribute("Include").Value.Contains("Dummy.aspx")
         select contentEl
).Count() > 0
    select element;

Sample III-a: Embedding of nested query in filter function

Retrieve all ItemGroup elements which contains Content elements with Include attribute set on value "Dummy.aspx".
This example demonstrates usage of filter function instead of nested queries. It is more usable for debugging.

var contentItemGroup = from element in
    solutionXml.Elements(m_Namespace + "ItemGroup
    where
filter(element, "Dummy.aspx")
    select element;

private bool filter(XElement x, string specificAttributeValue)
{
  var contentEls =
  from contentEl in x.Elements(m_Namespace + "Content")
  where contentEl.Attribute("Include") != null &&
  contentEl.Attribute("Include").Value.Contains(specificAttributeValue)
  select contentEl;

 return contentEls.Count() > 0;
}

Visit: www.daenet.eu


Posted Dec 13 2007, 09:36 PM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.