As an alternative to solution 1 by
Graeme_Grant[
^], i'd like to say...
MSDN documentation is very good. I'd suggest to start with:
LINQ to XML Overview (C#)[
^]
LINQ to XML (C#)[
^]
Basic Queries (LINQ to XML) (C#)[
^], especially:
How to: Find an Element with a Specific Attribute (C#)[
^]
I'd resolve your issue this way:
string xcontent = @"<Root>
<Categories TopCategory='txt 1' MediumCategory='txt 2' SubCategory='txt 3' />
<Categories TopCategory='txt 4' MediumCategory='txt 4' SubCategory='txt 5' />
<Categories TopCategory='txt 7' MediumCategory='txt 8' SubCategory='txt 9' />
<Categories TopCategory='txt 10' MediumCategory='txt 11' SubCategory='txt 12' />
<Categories TopCategory='txt 13' MediumCategory='txt 14' SubCategory='txt 15' />
<Categories TopCategory='txt 16' MediumCategory='txt 17' SubCategory='txt 18' />
</Root>";
XmlReader rdr = XmlReader.Create(new StringReader(xcontent));
XDocument xdoc = XDocument.Load(rdr);
var result = xdoc.Descendants()
.Where(x=>(string)x.Attribute("SubCategory")=="txt 12");
Above code should return (
IEnumerable<XElement>
):
<Categories TopCategory="txt 10" MediumCategory="txt 11" SubCategory="txt 12" />