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

  • Home
  • SEARCH
  • 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 8773693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:22:03+00:00 2026-06-13T18:22:03+00:00

I suppose the solution is simple, but I haven’t found an answer despite searching

  • 0

I suppose the solution is simple, but I haven’t found an answer despite searching the internet extensively. Please help! The problem is : my xml query is not returning the required result if the required attribute is not inside the first element of similarly named elements. For example, this code is not returning anything:

XElement xelement = XElement.Load("~/Employees.xml"));
        var homePhone = from phoneno in xelement.Elements("Employee")
               where (string)phoneno.Element("Phone").Attribute("Type") == "Work"
        select phoneno;
foreach (XElement xEle in homePhone)
        TextBox1.Text= xEle + "/n";

However, if I substitute “Work” with “Home”, I get the required result. In fact, I should get exactly the same result with “Work”. Can someone please explain how to do this?

The xml template is:

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
  <Employee>
    <EmpId>1</EmpId>
    <Name>Sam</Name>
    <Sex>Male</Sex>
    <Phone Type="Home">423-555-0124</Phone>
    <Phone Type="Work">424-555-0545</Phone>
    <Address>
      <Street>7A Cox Street</Street>
      <City>Acampo</City>
      <State>CA</State>
      <Zip>95220</Zip>
      <Country>USA</Country>
    </Address>
  </Employee>
  <Employee>
    <EmpId>2</EmpId>
    <Name>Lucy</Name>
    <Sex>Female</Sex>
    <Phone Type="Other">143-555-0763</Phone>
    <Phone Type="Work">434-555-0567</Phone>
    <Address>
      <Street>Jess Bay</Street>
      <City>Alta</City>
      <State>CA</State>
      <Zip>95701</Zip>
      <Country>USA</Country>
    </Address>
  </Employee>
  <Employee>
    <EmpId>3</EmpId>
    <Name>Kate</Name>
    <Sex>Female</Sex>
    <Phone Type="Home">166-555-0231</Phone>
    <Phone Type="Other">233-555-0442</Phone>
    <Address>
      <Street>23 Boxen Street</Street>
      <City>Milford</City>
      <State>CA</State>
      <Zip>96121</Zip>
      <Country>USA</Country>
    </Address>
  </Employee>
  <Employee>
    <EmpId>4</EmpId>
    <Name>Chris</Name>
    <Sex>Male</Sex>
    <Phone Type="Work">564-555-0122</Phone>
    <Phone Type="Other">442-555-0154</Phone>
    <Address>
      <Street>124 Kutbay</Street>
      <City>Montara</City>
      <State>CA</State>
      <Zip>94037</Zip>
      <Country>USA</Country>
    </Address>
  </Employee>
</Employees>
  • 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-13T18:22:05+00:00Added an answer on June 13, 2026 at 6:22 pm

    XElement.Element(“name”) will return the FIRST element of the given name when there is more than one. You really want this:

    var employeesWithHomePhone = from e in xdocument.Root.Elements("Employee")
                                 where e.Elements("Phone").Any( p => (string)p.Attribute("Type") == "Home") 
                                 select e;
    

    And just change “Home” to “Work” to get the ones with a Work number

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

Sidebar

Related Questions

I'm looking for an elegant solution to this very simple problem in MATLAB. Suppose
I have searched all around to find a solution to my problem, but i
Suppose my solution have two project: myApp: silverlight application project: the default application App
Suppose I have a solution inside a TFS collection that contains 3 projects. Ho
Suppose I have a simple model, such as Record: @Model public class Record {
i'm having this problem: Suppose you've a blog application and you want to display
I'm not sure that regexes are the best solution here, but they seem logical
What I am trying to do seems simple, but I can't find a way.
So, suppose I have a simple array of sentences. What would be the best
I have a problem using multiple (2) jQuery plugins. I've googled a little but

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.