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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:47:53+00:00 2026-06-04T16:47:53+00:00

I am attempting to get a Select List from an XML string… <?xml version=1.0

  • 0

I am attempting to get a Select List from an XML string…

<?xml version="1.0" encoding="utf-8"?>
<selectListItemDefinition id="UPMCTypes">
  <item key="bla1" value="bla" />
  <item key="bla2" value="bla" />
  <item key="bla3" value="bla" />
  <item key="bla4" value="bla" />
  <item key="bla5" value="bla" />
  <item key="bla6" value="bla" />
  <item key="bla7" value="bla" />
  <item key="bla8" value="bla" />
  <item key="bla9" value="bla" />
  <item key="bla10" value="bla" />
  <item key="bla11" value="bla" />
</selectListItemDefinition>

That would be the XML string that I am trying to turn into a SelectList
Here is how I am trying to do it…

List<SelectListItem> SListItem =
(
    from xmlSList in xDoc.Descendants("selectListItemDefinition")
    let listItem = xmlSList.Elements("item")
    select new SelectListItem
    {
        Key = xmlSList.Attribute("key").Value,
        Value = xmlSList.Attribute("value").Value
    }
).ToList();

This only gets the first Key-Value one for me.

Key    "blah1"  string
Value  "blah"   string

Now I think it’s because I am only getting one Element here? But I am not sure what I would do to get this right.

  • 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-04T16:47:54+00:00Added an answer on June 4, 2026 at 4:47 pm

    I think you have a problem in your query, as you should be getting an exception.

    You are trying to select the attributes “key” and “value” of <selectListItemDefinition>, but they don’t exist. And you are not actually using listItem in your select clause.

    Your first line is returning an IEnumerable<XElement> of all nodes called “selectListItemDefinition”. Your second line is just taking that IEnumerable<> and assigning the child elements called “item” it to variable named listItem.

    When you should be doing is a second query to iterate over the <selectListItemDefinition> elements instead of assigning it.

    So replace let listItem = xmlSList.Elements("item") with from listItem in xmlSList.Elements("item") and it should behave as you expect.

    the full query would be:

    var SListItem = 
                    (
                        from xmlSList in xDoc.Descendants("selectListItemDefinition")
                        from listItem in xmlSList.Elements("item")
                        select new SelectListItem
                        {
                            Key = listItem .Attribute("key").Value,
                            Value = listItem .Attribute("value").Value
                        }
                    ).ToList();
    

    And if you are 100% certain that the only elements called <item> in your XML document are the ones you want, you can replace the first and second lines with a single one from listItem in xDoc.Descendants("item") and you will get the same behavior.

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

Sidebar

Related Questions

I am attempting to select from a List using a linq expression where the
I'm attempting to get a dropdown list to select the correct value when the
I am attempting to get some information from a website, the info that I
I am attempting to get distinct values from a datatable column. here is my
I'm attempting to get the cell value from a gridview but running into a
I am attempting to get a list of all friend messages posted by the
I am attempting to get all the data from a MySQL db with PHP,
I am attempting to get a list of items along with the order information
Alright, so what I'm attempting to do here, is select from a datatable all
I'm attempting to use a prepared statement to select a value from an SQL

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.