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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:54:34+00:00 2026-05-10T22:54:34+00:00

I’ve got a simple class that inherits from Collection and adds a couple of

  • 0

I’ve got a simple class that inherits from Collection and adds a couple of properties. I need to serialize this class to XML, but the XMLSerializer ignores my additional properties.

I assume this is because of the special treatment that XMLSerializer gives ICollection and IEnumerable objects. What’s the best way around this?

Here’s some sample code:

using System.Collections.ObjectModel; using System.IO; using System.Xml.Serialization;  namespace SerialiseCollection {     class Program     {         static void Main(string[] args)         {             var c = new MyCollection();             c.Add('Hello');             c.Add('Goodbye');              var serializer = new XmlSerializer(typeof(MyCollection));             using (var writer = new StreamWriter('test.xml'))                 serializer.Serialize(writer, c);         }     }      [XmlRoot('MyCollection')]     public class MyCollection : Collection<string>     {         [XmlAttribute()]         public string MyAttribute { get; set; }          public MyCollection()         {             this.MyAttribute = 'SerializeThis';         }     } } 

This outputs the following XML (note MyAttribute is missing in the MyCollection element):

<?xml version='1.0' encoding='utf-8'?> <MyCollection xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>     <string>Hello</string>     <string>Goodbye</string> </MyCollection> 

What I want is

<MyCollection MyAttribute='SerializeThis'      xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'      xmlns:xsd='http://www.w3.org/2001/XMLSchema'>     <string>Hello</string>     <string>Goodbye</string> </MyCollection> 

Any ideas? The simpler the better. Thanks.

  • 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. 2026-05-10T22:54:34+00:00Added an answer on May 10, 2026 at 10:54 pm

    Collections generally don’t make good places for extra properties. Both during serialization and in data-binding, they will be ignored if the item looks like a collection (IList, IEnumerable, etc – depending on the scenario).

    If it was me, I would encapsulate the collection – i.e.

    [Serializable] public class MyCollectionWrapper {     [XmlAttribute]     public string SomeProp {get;set;} // custom props etc     [XmlAttribute]     public int SomeOtherProp {get;set;} // custom props etc     public Collection<string> Items {get;set;} // the items } 

    The other option is to implement IXmlSerializable (quite a lot of work), but that still won’t work for data-binding etc. Basically, this isn’t the expected usage.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are

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.