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>