I have this xml file where I am trying to get some tags from.
Here is the thing… I want to extract the tags that has a specific attribute with a specific value…
Here is an example
<root>
<input class="x">Data</input>
<input>Data2</input>
<input name="y">Data3</input>
<input class="z">Data4</input>
</root>
I want to get all the “input” tags that has the attribute “class”.
List<XElement> selected = xmlDoc.Descendants("input").Where(t => t.Element("input").Attributes("class") != null).ToList();
but it gives me a null reference exception in the lambda expression… Would you please help me?
t.Attributes("class")will never return null. Uset.Attribute("class")instead (or.Where(t => t.Attributes("class").Any())).You don’t also need
t.Element("input")or simply
with the help of XPATH