tags:

views:

227

answers:

3

I'm trying to capture Elements with a child decendent attribute == value

class Program
{
    static void Main(string[] args)
    {
        XDocument doc = XDocument.Load(@"path to doc");
        var query = from q in doc.Elements("Candidates").Elements("Candidates")
                    //How to filter based on descendant attribute value
                    where (string)q.Descendants("CandidatesPropertyValue")
                    .Attributes["PropertyValue"] != "Consumer Sales & Information"
                    //? this doesn't work obviously
                    select q;
        string type;
        string val;
        foreach (var record in query)
        {
            foreach (XAttribute a in record.Element("Candidates").Attributes())
            {
                Console.WriteLine("{0} = \"{1}\"", a.Name.ToString(), a.Value.ToString());
            }
            foreach (XElement e in record.Descendants())
            {
                type = (string)e.Attribute("PropertyType").Value;
                val = (string)e.Attribute("PropertyValue").Value;
                Console.WriteLine("     {0} = \"{1}\"", type, val);
            }
        }
        Console.ReadLine();
    }

<CandidatesRoot>
      I WANT THIS ELEMENT
- <Candidates ...bunch of attributes...>
  <CandidatesPropertyValue PropertyType="Type1" PropertyValue="Value1" /> 
  <CandidatesPropertyValue PropertyType="Type2" PropertyValue="Value2" /> 
  <CandidatesPropertyValue PropertyType="Type3" PropertyValue="Value3" /> 
  <CandidatesPropertyValue PropertyType="Type4" PropertyValue="Value4" /> 
  <CandidatesPropertyValue PropertyType="Type5" PropertyValue="Value5" /> 
  </Candidates>
      BUT I DON'T WANT THIS ONE
- <Candidates ...bunch of attributes...>
  <CandidatesPropertyValue PropertyType="Type1" PropertyValue="Value1" />
  <CandidatesPropertyValue PropertyType="LineOfBusiness" PropertyValue="Consumer Sales & Information" />
  <CandidatesPropertyValue PropertyType="Type2" PropertyValue="Value2" /> 
  <CandidatesPropertyValue PropertyType="Type3" PropertyValue="Value3" /> 
  <CandidatesPropertyValue PropertyType="Type4" PropertyValue="Value4" /> 
  <CandidatesPropertyValue PropertyType="Type5" PropertyValue="Value5" /> 
  </Candidates>
A: 

I'm going to venture a guess and say you need to change it to:

where (string)q.Descendants("CandidatesPropertyValue")
    .Attributes("PropertyValue").SingleOrDefault()

if it needs at least one descendant without that value:

where q.Descendants("CandidatesPropertyValue")
    .Attributes("PropertyValue")
    .Any(a => a.Value != "Consumer Sales & Information")
Yuriy Faktorovich
what if they're more than one descendant? For instance: my xml has an attribute called "PropertyType" which I could call out to identify that attribute "PropertyValue" to match
Chris
I had to change the 'from' statement using 'Descendants' instead of 'Element'var query = from q in doc.Descendants("Candidates")
Chris
A: 

This seeks all elements named Candidates who have at least one descendant named CandidatesPropertyValue with at least one attribute named PropertyValue with value "Consumer Sales & Information":

var query = from q in doc.Elements("Candidates").Elements("Candidates")
            where q.Descendants("CandidatesPropertyValue")
                   .Any(p => p.Attributes("PropertyValue")
                       .Any(a => a.Value == "Consumer Sales & Information"))
            select q;
Alex Bagnolini
+2  A: 

You can use XPath

using System.Xml.XPath;

...
String val = "\"Consumer Sales & Information\"";
String xpath = String.Format(".//CandidatesPropertyValue[@PropertyValue= {0}]", val);
doc.XPathSelectElements(xpath);
Dynami Le Savard