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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:54:18+00:00 2026-06-17T13:54:18+00:00

Had a quick question: am currently trying to learn how to work with XML

  • 0

Had a quick question: am currently trying to learn how to work with XML files and wanted to run a question by you:

I have run into a bit of an issue that I could certainly use some help with. I have created a simple little XML file (Users.XML) below:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <users>
      <user>
        <firstname>Might</firstname>
        <lastname>Guy</lastname>
        <username>SomeUser</username>
      </user>
      <user>
        <firstname>Bob</firstname>
        <lastname>Marley</lastname>
        <username>BobMarley</username>
      </user>       
    </users>

What I want to do is find a way to query that file to get specific information about a user. First name, last name, doesn’t matter. Now, after poking around I found what many folks considered to be one of the better ways is:

string firstName;

var data = from item in doc.Descendants("users")
                   where item.Element("user").Element("username").Value == "SomeUser"
                   select new
                   {
                       fname = item.Element("user").Element("firstname").Value
                   };

//Checking to see if anything was found. I know this isn't necessary to pull
//the information. I'm just doing this so I can know to throw an error to the
//screen if none is found
    if (data.Count() > 0)
//Now pull the data. Should only be 1 hit
        foreach (var p in data)
        {
            firstName = p.ToString();
        }
    else
        MessageBox.Show("No such user found");

Now, this example code does give me the first name of the user. BUT, the actual content of firstName is “fname = Might”. All I want it to contain is “Might”. Of course, if I drop the “fname =” from the select then it throws an error about not naming the field, so I sort of hit a wall. I tried googling for a solution, but since I’m not sure WHAT I’m googling it hasn’t brought me much luck.

So I have 3 questions. If it would be easier just to point me in the direction of a tutorial that contains the information, I would also be thrilled with that! Not just looking for an easy fix, looking to figure out why this is how it is since I’m trying to learn XML and not just complete a task.

-A) Is it possible using this method to simply have firstName contain “Might”, and not “fname = Might”. If so, how? If not, is there another way to do it that would?

-B) Why do I need the “fname =”? Is this something to do with LINQ, or is it part of something else entirely? Besides LINQ/XML stuff, I haven’t been able to find examples of this anywhere else yet so I haven’t been able to get a more detailed explanation on why it is like it is.

-C) Is this an acceptable method to go about what I’m trying to do in the example, aka query an XML file for a specific piece of information based on some field (the username, in this case)? If not, could you point me towards whatever method might be better?

  • 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-17T13:54:19+00:00Added an answer on June 17, 2026 at 1:54 pm

    The problem here is that you’re using p.ToString() instead of p.fname. How’s this:

    string firstName, lastName;
    
    var data = from item in doc.Descendants("user")
               where item.Element("username").Value == "SomeUser"
               select new
               {
                   fname = item.Element("firstname").Value,
                   lname = item.Element("lastname").Value
               };
    
    var p = data.FirstOrDefault();
    if(p != null)
    {
        firstName = p.fname;
        lastName = p.lname;
    }
    else
    {
        MessageBox.Show("No such user found");
    }
    

    To answer your question B, you do not need to use the fname = syntax. If you only want one value, instead of:

       select new
       {
           fname = item.Element("firstname").Value,
       };
    

    You could just use:

       select item.Element("firstname").Value;
    

    The select new { key = value, key2 = value2 } syntax allows you to put several values into an object of an anonymous class, from which you can easily pull out the values you need, by name.

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

Sidebar

Related Questions

I had a quick question about cookie security that I wanted to run by
Had a quick question: I have 2 view controllers, and their associated views. The
I had a quick question regarding RegEx... I have a string that looks something
I am new to Ruby and had a quick question. I am trying to
I've been playing with Spring and had a quick question... I have a loop
Alrighty, coding in Perl and just had a quick question. I have class created
I had a quick question. I am trying to get an user-authentication form to
I had a quick question on static blocks in AS3. I have a library
Had a quick question.. Googled but nothing worthwhile found.. I have a simple type
I just had a quick question about how stack variables versus heap variables work.

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.