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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:14:25+00:00 2026-06-06T17:14:25+00:00

I have class like this below shown. which contains the shopping items where the

  • 0

I have class like this below shown. which contains the shopping items where the number can vary from 0 to n.

namespace SerializationPOC
{

   public class ShoppingItems
   {
     public string CustomerName { get; set; }
     public string Address { get; set; }
     public List<Item> Items { get; set; }
   }

   public class Item
   {
    public string Name { get; set; }
    public string Price { get; set; }
   }
}

Is it possible to get the class serialized like to get the XML Schema like below.

<?xml version="1.0" encoding="utf-8" ?>
<ShoppingItems>
<CustomerName>John</CustomerName>
<Address>Walstreet,Newyork</Address>
<Item1>Milk</Item1>
<Price1>1$</Price1>
<Item2>IceCream</Item2>
<Price2>1$</Price2>
<Item3>Bread</Item3>
<Price3>1$</Price3>
<Item4>Egg</Item4>
<Price4>1$</Price4>


<Item..n>Egg</Item..n>
<Price..n>1$</Price..n>
</ShoppingItems>

I would like to know if this can be achieved by using the Serilization if not whats the best way to achieve this Schema?

  • 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-06T17:14:27+00:00Added an answer on June 6, 2026 at 5:14 pm

    There is no standard serializer that supports that layout. You will have to do it yourself. Personally, I would say “you’re doing it wrong”; I strongly suggest (if it is possible) using a format like

    <Item name="IceCream" Price="1$"/>
    

    or

    <Item><Name>IceCream</Name><Price>1$</Price></Item>
    

    both of which would be trivial with XmlSerializer.

    LINQ-to-XML is probably your best option, something like:

    var items = new ShoppingItems
    {
        Address = "Walstreet,Newyork",
        CustomerName = "John",
        Items = new List<Item>
        {
            new Item { Name = "Milk", Price = "1$"},
            new Item { Name = "IceCream", Price = "1$"},
            new Item { Name = "Bread", Price = "1$"},
            new Item { Name = "Egg", Price = "1$"}
        }
    };
    
    var xml = new XElement("ShoppingItems",
        new XElement("CustomerName", items.CustomerName),
        new XElement("Address", items.Address),
        items.Items.Select((item,i)=>
            new[] {
                new XElement("Item" + (i + 1), item.Name),
                new XElement("Price" + (i + 1), item.Price)}))
        .ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like this - class Messages { ... LinkedList<String> inputs; LinkedList<String>
I have a class like this: abstract class CrudResource extends Controller { type ResourceIdType
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have a class like this public class foo { private void getThread() {
I have a class like this: public class Contest { List<ContestTeam> Teams { get;
I have a class like this: class A { private: B* ptr; } But
I have a class like this class GUI : public QWidget, public QThread When
I have a class like this, public class person { name Name {set; get;}
So I have a class like this: class Encoder(object): movie = None def __init__(self,
I have an ASP.NET GridView which is populated from a SQL Server database. This

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.