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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:52:03+00:00 2026-06-10T13:52:03+00:00

I want to deserialize serialized XML containing collection of CDATA. XML looks like this:

  • 0

I want to deserialize serialized XML containing collection of CDATA.

XML looks like this:

<User Index="1">
   <Address><![CDATA[My street 1, 1234, My city]]></Address>
   <Address><![CDATA[My next street 1, 5678, My another city]]></Address>
</User>

Since I want to add new address directly as string like this:

var user = new User();
user.Addresses.Add("My street 1, 1234, My city");
user.Addresses.Add("My next street 1, 5678, My another city");

my user class looks like that:

[XmlRoot("User", IsNullable = true)]
public class User
{
    [XmlIgnore]
    private XmlDocument xmlDoc;

    [XmlIgnore]
    private Collection<string> addresses;

    [XmlIgnore]
    private Collection<XmlNode> addressesNode;

    public User()
    {
        xmlDoc = new XmlDocument();
        addresses = new Collection<string>();
    }

    [XmlAttribute("Index")]
    public int Index { get; set; }

    [XmlElement("Address", IsNullable = true)]
    public Collection<XmlNode> AddressesNode
    {
        get
        {
            // This will create CDATA in address node. String values for
            // CDATA are in addresses collection.
            // This is causing problem.
            return new Collection<XmlNode>(addresses.Select(a => xmlDoc.CreateCDataSection(a)).ToArray());
        }
        set
        {
            // Set also address collection of string to the same values.
            addresses = new Collection<string>(value.Select(a => a.Value).ToArray());

            addressesNode = value;
        }
    }

    [XmlIgnore]
    public Collection<string> Addresses
    {
        get
        {
            return addresses;
        }
    }
}

Serializing this works fine. But while deserializing, exception is thrown

 "Collection is read-only."
  • 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-10T13:52:05+00:00Added an answer on June 10, 2026 at 1:52 pm

    When you create a new Collection passing the IList as a parameter, it creates the collection as a wrapper around the original list. It does NOT copy it to a new list (see here). In your addressNode setter, you create a new collection as a wrapper around your collection. You may change this to a new List instead, since the List constructor does create a new list rather than wrapping the original one (see here).

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

Sidebar

Related Questions

I want to deserialize some XML that looks like this: XML: <bookings> <booking> <timeStart>2012/7/2
I've got a class that when serialized to XML looks like this (generalized for
I got an XML input string and want to deserialize it to an object
Currently, we use a giant configuration object that is serialized to/from XML. This has
I want to deserialize the xml string into an object of the type class
Assuming an XML like this: <my:Root xmlns:my=http://foo/bar> <my:FieldBasic>content</my:FieldBasic> <my:FieldComplex> <html xml:space=preserve xmlns=http://www.w3.org/1999/xhtml> <div><h1>content</h1></div> </html>
I have a bunch of objects that were serialized to XML. Now I want
Generally, this is how I deserialize an XML file: string location = C:\\test.xml; XmlObjectClass
Let's assume I want to deserialize this (I've removed the namespaces to make things
I have an XML dox like this: <?xml version=1.0 encoding=utf-8?> <Server Manufacturer=SQL Version=1> <Database

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.