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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:48:53+00:00 2026-06-12T06:48:53+00:00

I want to serialize an object as xml that contains other custom classes. From

  • 0

I want to serialize an object as xml that contains other custom classes. From what I understand (I’ve been reading MSDN and SO mostly), the XmlSerializer doesn’t take this into account.

This is the line that’s confusing me:

XML serialization serializes only the public fields and property values of an object into an XML stream. XML serialization does not include type information. For example, if you have a Book object that exists in the Library namespace, there is no guarantee that it will be deserialized into an object of the same type.

Taken from MSDN, here

For example, I want to serialize an object of type Order, but it contains a list of Products, and each one contains an object of type Category:

class Order
{
    List<Product> products;
}

class Product
{
    Category type;
}

class Category
{
    string name;
    string description;
}

And I want my Order object to be serialized like so:

<Order>
    <Product>
        <Category Name="">
            <Description></Description>
        </Category>
    </Product>
    <Product>
        <Category Name="">
            <Description></Description>
        </Category>
    </Product>
<Order>

Does the XmlSerializer already do this? If not, is there another class that does or do I have to define the serialization process myself?

  • 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-12T06:48:54+00:00Added an answer on June 12, 2026 at 6:48 am

    An Order can be seen as a list of Products, a Product as a list of Categories (because it can pertain to multiple categories).

    You can try using

    //...
    using System.Xml;
    using System.Xml.Serialization;
    //...
    
    [XmlRoot("Order")]
    public class Order
    {
        [XmlArrayItem(ElementName = "Product", Type = typeof(Product))]
        public List<Product> Products;
    }
    
    public class Product
    {
        [XmlArrayItem(ElementName = "Category", Type = typeof(Category))]
        public List<Category> Categories;
    }
    
    public class Category
    {
        [XmlAttribute("Name")]
        public string name;
    
        [XmlElement("Description")]
        public string description;
    }
    

    The only trade-off is, that the <Products> and <Categories> (plural) tags will be visible, because the variables are named that way, but from a point of view of parsing the XML afterwards, that’s not an issue.
    If any other fields show up in your XML that you don’t want, you can have [XmlIgnore()] precede them.

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

Sidebar

Related Questions

I have a custom principal object that I want to be able to serialize
I want to serialize an object into an xml and I want the filename
Currently, we use a giant configuration object that is serialized to/from XML. This has
I want to serialize my object to xml and then to a string. public
How do I serialize a 'Type'? I want to serialize to XML an object
I have an XML document that has been created using utf-8 encoding. I want
I want a method to return an XML Serialized Typed object. Is there an
I want to serialize an object and store it inside sdcard under my project
I want to serialize an object of type Person . I want to use
I want to store a .NET object into Azure Blob Storage. Currently I serialize

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.