XML
<Questions>
<Question>
<Id>1</Id>
<Text>aaa</Text>
</Question>
<Question>
<Id>2</Id>
<Text>bbb</Text>
</Question>
</Questions>
Code
question="aaa";
var doc = XDocument.Load(Server.MapPath(".") + "\\Questions.xml");
var elements = from element in doc.Descendants("Question")
let txt = element.Element("Text")
where question.CompareTo (txt)==0
select new
{
Id = element.Element("Id").Value,
};
This code elements.count()==>0
I would Like that select from xml where txt=='aaa'
The line
let txt = element.Element("Text")returns a XElement instead of the text so yourCompareTocondition will blow up instead of checking the text values.Instead you can get the value of the node via the
.Valueproperty.The line
var elements = from element in doc.Descendants("Question")will successfully lookup the elements but as a practice you might want to go from the root node or relative hierarchy.The rest of the code seems fine (less exception handling).
The following worked for me…