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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:07:04+00:00 2026-06-13T23:07:04+00:00

XElement documentRoot = XElement.Parse(@<OrdersReport Date=’2012-08-01′> <SettingHeader> <SettingId>1</SettingId> </SettingHeader> <Client> <ClientId>1</ClientId> <Orders> <Order> <OrderNumber>1</OrderNumber> <ShipAddress>123

  • 0
XElement documentRoot = XElement.Parse(@"<OrdersReport Date='2012-08-01'>
                <SettingHeader>
                  <SettingId>1</SettingId>
                </SettingHeader>
                  <Client>
                    <ClientId>1</ClientId>
                    <Orders>
                      <Order>
                        <OrderNumber>1</OrderNumber>
                        <ShipAddress>123 Main St.</ShipAddress>
                        <ShipCity>MyCity</ShipCity>
                        <ShipState>AZ</ShipState>
                      </Order>
                      <Order>
                        <OrderNumber>2</OrderNumber>
                        <ShipAddress>111 Main St.</ShipAddress>
                        <ShipCity>OtherCity</ShipCity>
                        <ShipState>AL</ShipState>
                      </Order>
                      <OrderCancelled>
                        <OrderNumber>3</OrderNumber>
                        <ShipAddress>222 Main St.</ShipAddress>
                        <ShipCity>CancelledCity</ShipCity>
                        <ShipState>AL</ShipState>
                      </OrderCancelled>
                    </Orders>
                    <Returns>
                      <Amount>
                        <OrderId>2</OrderId>
                        <OrderId>3</OrderId>
                        <OrderId>21</OrderId>       
                      </Amount>
                    </Returns>
                  </Client>
                  <Client>
                    <ClientId>2</ClientId>    
                  </Client>
                </OrdersReport>");

Code:

        var orders = documentRoot.Descendants("Order")
                       .Concat(documentRoot.Descendants("OrderCancelled"))
                       .Select(x => new
                       {
                           OrderNumber = (int)x.Element("OrderNumber"),
                           ShipAddress = (string)x.Element("ShipAddress"),
                           ShipCity = (string)x.Element("ShipCity"),
                           ShipState = (string)x.Element("ShipState")
                       });

        foreach (var o in orders)
        {
            Console.WriteLine(o.OrderNumber + " " + o.ShipAddress + " " 
                + o.ShipCity);
        }

Output:

1 123 Main St. MyCity
2 111 Main St. OtherCity
3 222 Main St. CancelledCity
3 222 Main St. CancelledCity

How do I append ClientId and SettingId like the below output?

Target output:

1 123 Main St. MyCity ClientId 1 SettingId 1
2 111 Main St. OtherCity ClientId 1 SettingId 1
...
  • 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-13T23:07:05+00:00Added an answer on June 13, 2026 at 11:07 pm

    Try this:

    var orders = doc.Descendants("Order").Select(x => new
        {
            OrderNumber = (int)x.Element("OrderNumber"),
            ShipAddress = (string)x.Element("ShipAddress"),
            ShipCity = (string)x.Element("ShipCity"),
            ShipState = (string)x.Element("ShipState"),
            ClientId = (int)x.Ancestors("Client").First().Element("ClientId"),
            SettingId = (int)doc.Descendants("SettingId").First(),
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't seem to find how to appropriately parse this using XElement: <messages> <message subclass=a
Say I call XElement.Parse() with the following XML string: var xml = XElement.Parse(@ <?xml
I am using Xelement - Linq to XML to parse some an RSS feed.
XDocument doc = XDocument.Parse(_data) XElement root = new XElement(student); doc.Element(marks).Add(root); doc.Save(_data) the _data is
XElement config = XElement.Parse( @<Response SessionId='BEDF38F9ADAB4F029404C69E49951E73' xmlns='http://schemas.sample.com/sample.xsd'> <Status Success='true' Message='User is now logged in.'
` XElement config = XElement.Parse ( @<Response SessionId='426D9AEB1F684849A16D79A6CF48582B' xmlns='http://schemas.tmaresources.com/timssws60.xsd'> <Status Success='true' Message='Connected' ErrorCode='0' />
Is theres a way to Parse XML using XElement.Parse that won't convert entities to
void SaveFiles(out XElement Attachments) { Attachments = null; if (Uploader1.UploadedFiles.Count() > 0) { Attachments
I have the following XElement: <Assembly name=3> <Component name=2 /> </Assembly> I would like
I have an XElement object which has a number of attributes and I simply

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.