Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6327099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:13:02+00:00 2026-05-24T17:13:02+00:00

Trying to use a query like this: var checkName = from nn in xdoc.Root.Elements(string)

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:13:03+00:00Added an answer on May 24, 2026 at 5:13 pm

    This query will just all the elements that match your condition:

    var checkName = from nn in xdoc.Root.Elements("string")
                            where nn.Attribute("id").Value.Equals(newTag)
                            select nn;
    

    And then your if statement is as simple as checking if any such elements exist:

    if (checkName.Any())
    {
        // Code if condition is met by any tag here
    }
    

    If you really need a bool instead, you can combine the query like so:

    bool anyMatches = xdoc.Root.Elements("string")
                       .Where(x => x.Attributes("id").Value.Equals(newTag)).Any();
    

    Finally, for completeness sake, you can move the predicate from the Where() into Any():

    bool anyMatches = xdoc.Root.Elements("string")
                           .Any(x => x.Attributes("id").Value.Equals(newTag));
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a Linq query that will retrieve a string value from
I' trying to use a Linq query to find and set the selected value
I am trying to use this MySQL query: SET @a:=0; UPDATE tbl SET sortId=@a:=@a+1
I have a query similar to this: SELECT itemID FROM itemTable WHERE itemID LIKE
I am trying to use an update query to update the first 3 characters
I am trying to use JDBC and my query is working in some cases
I'm trying to use Google Analytics API to query internal searches that happen on
I am trying to use Zend_Db_Select to write a select query that looks somewhat
I'm trying to use Subsonic 3.0 but with every query I try it gives
I am trying to use a SQL Server 2008 Ranking Function on a query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.