Trying to use a query like this:
var checkName = from nn in xdoc.Root.Elements("string")
where nn.Attribute("id").Value.Equals(newTag)
select thisbool = true;
To see if, in my XML, there exists a node string where the value of attribute id equals this string variable newTag. If there does NOT exist such a string node, I would like to return null, or something that I can check using an if statement directly below so that I may disallow a particular change to be made, i.e.
if (thisbool)
{
MessageBox.Show("The string ID you entered is already in use. Please enter a different string ID.");
tagBox.Text = undoTag;
return;
}
This is my current setup. I also tried just selecting nn and using if(nn != null) but nothing seems to work. I’m sorry if this is a newb question — I’m entering a bit of a time crunch, and I did indeed try to find an answer and test things out for 45min-1 hour.
This query will just all the elements that match your condition:
And then your if statement is as simple as checking if any such elements exist:
If you really need a
boolinstead, you can combine the query like so:Finally, for completeness sake, you can move the predicate from the
Where()intoAny():I pesonally prefer one of the former two methods, since I think they spell out more clearly what’s going on. Up to you which you prefer, of course.