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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:13:58+00:00 2026-05-12T09:13:58+00:00

OK, I asked for how to return a Linq query results as XML, and

  • 0

OK, I asked for how to return a Linq query results as XML, and I got the answer here.

But there’s one little problem: the results do not get grouped logically within the XML. For example:

XElement xml = new XElement("States",
  from s in MyStates
  from cy in s.Counties
  from c in cy.Cities
  where s.Code == "NY"
  orderby s.Code, cy.Name, c.Name
  select new XElement("State",
    new XAttribute("Code", s.Code),
    new XAttribute("Name", s.Name),
    new XElement("County",
      new XAttribute("Name", cy.Name),
      new XElement("City",
        new XAttribute("Name", c.Name)
      )
    )
  )
);

Console.WriteLine(xml);

The output is of the form:

<State Code="NY" Name="New York ">
  <County Name="WYOMING">
    <City Name="WARSAW" />
  </County>
</State>
<State Code="NY" Name="New York ">
  <County Name="WYOMING">
    <City Name="WYOMING" />
  </County>
</State>
<State Code="NY" Name="New York ">
  <County Name="YATES">
    <City Name="BELLONA" />
  </County>
</State>
<State Code="NY" Name="New York ">
  <County Name="YATES">
    <City Name="MIDDLESEX" />
  </County>
</State>
<State Code="NY" Name="New York ">
  <County Name="YATES">
    <City Name="PENN YAN" />
  </County>
</State>
<State Code="NY" Name="New York ">
  <County Name="YATES">
    <City Name="RUSHVILLE" />
  </County>
</State>

instead of:

<State Code="NY" Name="New York ">
  <County Name="WYOMING">
    <City Name="WARSAW" />
    <City Name="WYOMING" />
  </County>
  <County Name="YATES">
    <City Name="BELLONA" />
    <City Name="MIDDLESEX" />
    <City Name="PENN YAN" />
    <City Name="RUSHVILLE" />
  </County>
</State>

How do I get the results to appear as desired?

  • 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-05-12T09:13:58+00:00Added an answer on May 12, 2026 at 9:13 am

    To get the results you want, I believe you’ll have to nest LINQ to XML queries. On the outer query, you’ll have to query for distinct states…then an inner query to get the counties for that state…then another inner query to get the cities for that county.

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

Sidebar

Related Questions

I asked a question here about an Linq error that results from mixing Linq-To-SQL
(I asked this question in another way , and got some interesting responses but
I asked this question before, Here however I think I presented the problem poorly,
I asked a similar question about this previously, but I did not specify that
Probably this question was already asked before, but my google-fu and SO-Search did not
I asked a couple of coworkers about AnkhSVN and neither one of them was
I asked a somewhat related question but I want it to make it more
I asked here about a protocol that I was asked to implement, and how
i asked this a few weeks ago, but couldnt get any of the suggested
I am writing a LINQ query which has a sort of subquery. It looks

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.