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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:25:47+00:00 2026-05-20T16:25:47+00:00

I have products in a class that I want to save in a XML

  • 0

I have products in a class that I want to save in a XML file, the products is devided into categoris and I would like to save my products in a XML file formatet like below examlple. Products for category 1 writes under category one and products for category 2 under category two.

Hwo can I write a method that dose that?
Thanks in advance.

<Categories>
  <Category ID="1">
    <CategoryName>Categoriname1</CategoryName> 
    <Description>drinks, coffees, beers, ales</Description> 
    <Products>
      <Product>
        <ProductName>Coffe</ProductName> 
        <QuantityPerUnit>15 boxes x 60 bags</QuantityPerUnit> 
        <UnitPrice>25</UnitPrice> 
        <UnitsInStock>3</UnitsInStock> 
        <UnitsOnOrder>0</UnitsOnOrder> 
      </Product>
      <Product>
        <ProductName>Chang</ProductName> 
        <QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit> 
        <UnitPrice>19</UnitPrice> 
        <UnitsInStock>17</UnitsInStock> 
        <UnitsOnOrder>40</UnitsOnOrder> 
      </Product>
    </Products>
  </Category>
  <Category ID="2">
    <CategoryName>Condiments</CategoryName> 
    <Description>spreads, and seasonings</Description> 
    <Products>
      <Product>
        <ProductName>Productname</ProductName> 
  • 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-20T16:25:47+00:00Added an answer on May 20, 2026 at 4:25 pm

    One option is to use LINQ to XML. Assume you have these classes (where I have removed some properties to simplify the example):

    class Category {
      public Int32 Id { get; set; }
      public String Name { get; set; }
      public IEnumerable<Product> Products { get; set; }
    }
    
    public class Product {
      public String Name { get; set; }
    }
    

    You can create some test data:

    var categories = new[] {
      new Category {
        Id = 1,
        Name = "Category 1",
        Products = new[] {
          new Product { Name = "Coffee" },
          new Product { Name = "Chang" }
        }
      },
      new Category {
        Id = 2,
        Name = "Condiments",
        Products = new[] {
          new Product { Name = "Product 1" }
        }
      }
    };
    

    You can then create an XDocument from the test data:

    var xmlDocument = new XDocument(
      new XElement(
        "Categories",
        categories.Select(
          c => new XElement(
            "Category",
            new XAttribute("ID", c.Id),
            new XElement("CategoryName", c.Name),
            new XElement("Products",
              c.Products.Select(
                p => new XElement(
                  "Product",
                  new XElement("ProductName", p.Name)
                )
              )
            )
          )
        )
      )
    );
    

    To save it to a file you can use the Save method:

    xmlDocument.Save("Categories.xml");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Products:List<Product> class. I'd like to make it so that every time
I have a class that contains a member object. I would like to call
I have a model that I want to save in Django Admin class Product(models.Model):
i have a class and wish to serialize it into xml. the class contains
Here is what I want to do. I have a class that I have
I have a Products class. Now I want to add some kind of discount
I'm using C#. I have a products class with fields like sku, name, description....
I have an Offer class (NSManagedObject subclass) that I want to use to handle
I have a view model that contains a Product class type and an IEnumerable<
I have a viewmodel that contains a product and SelectList of categories. public class

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.