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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:43:24+00:00 2026-06-02T08:43:24+00:00

I am trying to serialize several lists of objects to xml. The lists are

  • 0

I am trying to serialize several lists of objects to xml. The lists are of different types but they all need to have some the same attributes on the top list object.

What I am trying to get is a ‘count’ on the top level and the name of the object for all the items in the list:

 <JobResult Count="2">
       <Job>
           <Id>1</Id>
       </Job>
       <Job>
           <Id>2</Id>
       </Job>
    </JobResult>

Then for another list:

 <PersonResult Count="1">
       <Person>
           <Id>1</Id>
       </Person>
 </PersonResult>

The code I am using is:

[XmlRoot()]
    public class Result<T>
    {
        [XmlElement()]
        public List<T> Items { get; set; }

        public Result()
        {
            this.Items = new List<T>();
        }

        [XmlAttribute("Count")]
        public int ItemCount
        {
            get
            {
                return this.Items.Count;
            }
            set
            {

            }
        }
    }
var jobs= new Result<Job>();
var persons= new Result<Person>();

What I am getting is:

<ResultOfJob Count="2">
       <Item>
           <Id>1</Id>
       </Item>
       <Item>
           <Id>2</Id>
       </Item>
    </ResultOfJob >

I have tried the attribute naming like this but get <_x007B_0_x007D_> instead of item.

[XmlElement({0})]
 public List<T> Items { get; set; }

What is the best way to handle naming the items dynamically?

  • 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-02T08:43:25+00:00Added an answer on June 2, 2026 at 8:43 am

    The way I ended up solving this is creating the result class in a similar fashion:

    public abstract class Result<T> 
    {
        [XmlIgnore]
        public abstract List<T> Items {get;set;}
    
        [XmlAttribute]
        public int ResultCount
        {
            get
            {
                return this.Items.Count;
            }
            set { }
        }
    
    
        public Result()
        {
            this.Items = new List<T>();
        }
    

    And then I Inherit this class for any object that I need to have the common ResultCount as an attribute. If I need to add any other common attributes to all of the results objects I can do it through the above Result class.

    Here Is an example inherited class:

    public class ChallengesResult : Result<ChallengeResource>
    {
        public ChallengesResult()
            : base()
        {
        }
    
    
        [XmlElement("Challenge")]
        public override List<ChallengeResource> Items { get; set; }
    }
    

    The [XmlIgnore] on the List in the Result class allows me to specify the name of the list items in the derived class with the [XmlElement] attribute.

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

Sidebar

Related Questions

I have some XML that I am trying serialize like so: string Value =
I'm trying to serialize some objects obtained from a 3rd Party .NET Lib to
I have to serialize several objects inheriting from WebControl for database storage. These include
im trying to serialize some object as xml and then read it back the
I am trying to serialize a class several of the data-members are Nullable objects,
I'm trying to serialize an object to XML that has a number of properties,
I have been trying to serialize a pretty big object. This object uses dictionaries
i'm trying to serialize DataTable to Json or XML. is it possibly and how?
I am trying to serialize/deserialize JSON in Android using GSON. I have two classes
I've been trying to serialize an XML (jQuery object) to string to POST it

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.