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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:53:32+00:00 2026-05-18T19:53:32+00:00

I have a List<Item> collection that I am trying to generate an xml file

  • 0

I have a List<Item> collection that I am trying to generate an xml file from using Linq to XML.

The List class is below:

public class Item
{
    public int Id { get; set; }
    public string ItemName {get; set;}
}

I need to get XML that looks like this:

<Items>
  <Item>
    <ID>1</ID>
    <Item_Name>Super Sale Item Name</Item_Name>
  </Item>
</Items>

Here’s the query I tried but am having no luck getting to work

XDocument xdoc = new XDocument(new XElement("Items"),
          _myItemCollection.Select(x => new XElement("Item",
                                            new XElement("ID", x.Id),
                                            new XElement("Item_Name", x.ItemName))));

I keep getting an error saying it would create invalid XML. Any ideas?

Error is

This operation would create an incorrectly structured document.

at System.Xml.Linq.XDocument.ValidateDocument(XNode previous, XmlNodeType allowBefore, XmlNodeType allowAfter)
at System.Xml.Linq.XDocument.ValidateNode(XNode node, XNode previous)
at System.Xml.Linq.XContainer.AddNodeSkipNotify(XNode n)
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XContainer.AddContentSkipNotify(Object content)
at System.Xml.Linq.XDocument..ctor(Object[] content)

  • 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-18T19:53:32+00:00Added an answer on May 18, 2026 at 7:53 pm

    Try this:

    using System;
    using System.Linq;
    using System.Xml.Linq;
    
    public class Item
    {
        public int Id { get; set; }
        public string ItemName { get; set; }
    }
    
    class Program
    {
        static void Main()
        {
            var collection = new[]
            {
                new Item {Id = 1, ItemName = "Super Sale Item Name"}
            };
    
            var xdoc = new XDocument(new XElement("Items",
                                    collection.Select(x => new XElement("Item",
                                            new XElement("ID", x.Id),
                                            new XElement("Item_Name", x.ItemName)))));
    
            Console.WriteLine(xdoc);
        }
    }
    

    The main thing you are missing is that the project of your collection to XElement needs to be nested in the first XElement (“Items”) rather than it’s sibling. Notice that new XElement("Items")... is changed to new XElement("Items", ...

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

Sidebar

Related Questions

I have a List<Item> collection that I am trying to generate an xml file
I have a list view that displays a collection of items, each item has
I have Collection List<Car> . How to compare each item from this collection with
i'm trying to remove an item from a one to many list and have
I am trying to populate a collection with data from a JSON file. I'm
Trying to deserialize an XML file from a 3rd party tool into a custom
I have a large collection of custom objects that I have retrieved from a
I have a list item with a button inside. When the button is shown,
I have a List item List<string> xmlValue = new List<string>(); In this I have
I have a list item with buttons in it like so: <mx:List contentBackgroundAlpha=0 baseColor=0x333333

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.