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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:12:22+00:00 2026-06-05T21:12:22+00:00

I have the following xml that I need to get 2 values (see *

  • 0

I have the following xml that I need to get 2 values (see * in comments) from within the same query:
The two values are associated and I can get one set () or the other () but not using a single query.

Update I updated the xml below to include 2 nodes that exist under the node and added the namespace

Ideally I would like to get them both in a single query into a Dictionary

<root xmlns="http://www.blah.net/xsd/layout/2003-10-16">
    <header/>
    <movement>
        <movementHeader>
            <desc>xyz</desc>
        </movementHeader>
        <detailHeader>
            <desc>abc</desc>
        </detailHeader>
        <detail>
            <!-- * need this value -->
            <code>90125</code>
            <subDetail>
                <!-- * and need this value at same time -->
                <amount>1200.00</amount>
            </subDetail>
        </detail>
        <detail>
            <!-- * need this value -->
            <code>90126</code>
            <subDetail>
                <!-- * and need this value at same time -->
                <amount>1300.00</amount>
            </subDetail>
        </detail>
        <detail>
            <!-- * need this value -->
            <code>9012</code>
            <subDetail>
                <!-- * and need this value at same time -->
                <amount>1400.00</amount>
            </subDetail>
        </detail>
    </movement>
  • 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-05T21:12:24+00:00Added an answer on June 5, 2026 at 9:12 pm

    You can project to an anonymous type that holds the properties you want:

    var results = xdoc.Descendants("detail")
                      .Select( x => new 
                       {
                           Code = x.Element("code").Value,
                           Amount = x.Element("subDetail")
                                     .Element("amount").Value 
                       });
    
    foreach (var item in results)
    {
        Console.WriteLine("Code = {0}, Amount = {1}", item.Code, item.Amount);
    }
    

    Tested and works, returns 3 results as expected.

    To add this to a dictionary just add ToDictionary() :

    var dict = xdoc.Descendants("detail")
                   .Select(x => new
                    {
                       Code = x.Element("code").Value,
                       Amount = x.Element("subDetail")
                                 .Element("amount").Value
                    }).ToDictionary(x => x.Code, x => x.Amount);
    

    Edit:

    To account for the XML namespace you have to declare and use it, updated example below:

    XNamespace ns = "http://www.blah.net/xsd/layout/2003-10-16";
    var dict = xdoc.Descendants(ns + "detail")
                   .Select(x => new
                   {
                      Code = x.Element(ns + "code").Value,
                      Amount = x.Element(ns + "subDetail")
                                .Element(ns + "amount").Value
                   }).ToDictionary(x => x.Code, x => x.Amount);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have xml that looks somewhat similar to the following. I need to get
I need to send a HTTP request (and get XML response) from Flash that
i have the following XML that i need to parse in C#: <data> <customer_id><![CDATA[1414301]]></customer_id>
I have a Spring application-context.xml with PropertyPlaceholderConfigurer to get properties' values from .properties file.
I have the following XML Document (that can be redesigned if necessary) that stores
I have the following XML and I want to process it so that I
Say I have the following XML <?xml version=1.0 encoding=utf-8?> <Person> <FirstName>Bjorn</FirstName> <LastName>Ellis-Gowland</LastName> </Person> That
I am parsing some XML that will have a link such as the following
I have the following code that serializes a DataTable to XML. StringWriter sw =
I have a line of PHP code that does the following: $xml = <xml><request>...[snipped

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.