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 7759485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:38:31+00:00 2026-06-01T13:38:31+00:00

I recently came across a situation where I had to use XPath as part

  • 0

I recently came across a situation where I had to use XPath as part of a Linq to XML query and found that it acts a bit strange. I am sure there are other ways of solving the problem using the native XElement API methods, but in this situation XPath was a much better fit for locating the specific XML text values that I needed as the document structure was quite complex. I eventually found a way to make it work (see below), but it was not very pretty, which usually means I missed something.

My question is how can you use XPathEvalute or some other XPath method to return a string value that can be used in a where statement or as part of a new object in the select method. I found a solution that uses a lot of casting and the Null coalesing operator in LINQ, but there has to be a cleaner way. The root of the problem is that the XPathEvaluate method would usually return an XText element (casted to a generic object), but Linq converts it to an IEnumerable so that it can delay the execution. This causes problem when you try to access the value of that object, as it does not get materialized until the query is executed.

Here is the example: Using the following XML document find all parents that have a Boy and Girl with the same name. This MUST be done using an XPath query, as in my situation the document structure was a lot complex.

//Use C# Program Language type in LinqPad
void Main()
{
    var xml = XElement.Parse(@"<Root>
        <Parent>
            <Boy>Anne</Boy>
        </Parent>
        <Parent>
            <Boy>Alex</Boy>
            <Girl>Alex</Girl>
        </Parent>
        <Parent>
            <Boy>Jay</Boy>
        </Parent>
    </Root>");

    var q1 = from e in xml.XPathSelectElements("/Parent")
             select e;

    q1.Dump();

    var q2 = from e in q1
             let boy = ((IEnumerable<Object>)e.XPathEvaluate("Boy/text()")).Cast<System.Xml.Linq.XText>().FirstOrDefault() ?? new System.Xml.Linq.XText("")
             let girl = ((IEnumerable<Object>)e.XPathEvaluate("Girl/text()")).Cast<System.Xml.Linq.XText>().FirstOrDefault() ?? new System.Xml.Linq.XText("")
             select new {t=boy.GetType(), b=boy, g=girl, same=(boy.Value==girl.Value)};

    q2.Dump();
}

Results are:

enter image description here

You can see that the second element is identified as having a boy and girl with the same name. Is there any cleaner way of doing this while still using XPath or am I stuck using the code from above.

  • 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-06-01T13:38:32+00:00Added an answer on June 1, 2026 at 1:38 pm

    Is it acceptable to use XPathSelectElement instead of XPathEvaluate?

    var q2 = from e in q1
             let boy = (string)e.XPathSelectElement("Boy")
             let girl = (string)e.XPathSelectElement("Girl")
             where boy.StartsWith("A") //Optional use in where statement
             select new { t = boy.GetType(), b = boy, g = girl, same = (boy == girl) };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across some weird looking class that had three constructors: class Class
I recently came across classes that use a configuration object instead of the usual
I recently came across a bit of not-well-tested legacy code for writing data that's
I recently came across a coding standard claiming that you should never use public
Recently I came across a situation where, while using a double-byte language, I had
I recently came across http://chromeexperiments.com/ . I found a couple of projects there that
I recently came across a Windows library called AHK that gives me great control
I recently came across the situation where I wanted a set of classes to
Recently I came across a situation where set theory and set math fit what
I came across some code recently that used a custom error handler to turn

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.