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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:21:59+00:00 2026-05-12T05:21:59+00:00

I have a class that has the following properties: public class Author { public

  • 0

I have a class that has the following properties:

public class Author {
  public string FirstName { get; set; }
  public string LastName { get; set; }
}

Next, I have a List of Authors like so:

List<Author> authors = new List<Author> ();

authors.add(
  new Author { 
    FirstName = "Steven",
    LastName = "King"
  });

authors.add(
  new Author { 
    FirstName = "Homer",
    LastName = ""
  });

Now, I am trying to use Linq to XML in order to generate the XML representing my Authors List.

new XElement("Authors",
  new XElement("Author", 
    from na in this.Authors
    select new XElement("First", na.First)))

The block above does not generate the XML as I expect it to. What I get is:

<Authors>
  <Author>
    <First>Steven</First>
    <First>Homer</First>
  </Author>
<Authors>

What I want the XML output to look like is:

<Authors>
  <Author>
    <First>Steven</First>
    <Last>King</Last>
  </Author>
  <Author>
    <First>Homer</First>
    <Last></Last>
  </Author>
</Authors>

Any help on how to get the XML to look as I need it to would be immensely appreciated!

  • 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-12T05:21:59+00:00Added an answer on May 12, 2026 at 5:21 am

    You need to pass the IEnumerable<XElement> query as the second parameter, not the “Author” string, like so:

    // Note the new way to initialize collections in C# 3.0.
    List<Author> authors = new List<Author> ()
    {
      new Author { FirstName = "Steven", LastName = "King" }),
      new Author { FirstName = "Homer", LastName = "" })
    };
    
    // The XML
    XElement xml = new XElement("Authors",
        from na in this.Authors
        select new XElement("Author",
            new XElement("First", na.FirstName),
            new XElement("Last", na.LastName)));
    

    That will give you the result you need.

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

Sidebar

Related Questions

Say I have an email class that has the following properties: public string From
So I have a class that has the following member variables. I have get
I have a class that has the following property that is generated by the
I have the following setup. Class, say, Car that has a CarPart (belongsTo=[car:Car]). When
I have a class that has an list<Book> in it, and those Book objects
I have a class that has an list<Book> in it, and those Book objects
I have a class that has an Action<string> called DisplayData ; In various points
I have a class generated from a WSDL that has a bunch of public
So I have a class that looks something like the following: public class MyClass
I have a DependencyObject class composition that looks like the following: public class A

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.