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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:30:30+00:00 2026-05-12T20:30:30+00:00

Is it possible to Deserialize the following piece of XML into a Dictionary<int,string> object?

  • 0

Is it possible to Deserialize the following piece of XML into a Dictionary<int,string> object?

XML:

<Config>

<DatabaseConnections>
     <Connection Name="Source Connection" Value="ConnectionStringValue" />
     <Connection Name="Target Connection" Value="ConnectionStringValue" />
<DatabaseConnections>

<Config>

I have a class which will have a property of Dictionary<int,string> {get;set;}

I would like to use the following C# code to do this:

XmlSerializer xs = new XmlSerializer(typeof(Config));

using(StringReader sr = new StringReader(rootnode.OuterXml))
{
      return (Config)xs.Deserialize(sr);
}

Are there any alternatives to doing this?

  • 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-05-12T20:30:31+00:00Added an answer on May 12, 2026 at 8:30 pm

    You cannot use the default XmlSerializer implementation to (de)serialize IDictionary objects to Xml. From the MSDN XmlSerializer documentation:

    The XmlSerializer gives special
    treatment to classes that implement
    IEnumerable or ICollection. A class
    that implements IEnumerable must
    implement a public Add method that
    takes a single parameter. The Add
    method’s parameter must be of the same
    type as is returned from the Current
    property on the value returned from
    GetEnumerator, or one of that type’s
    bases.

    The Dictionary<> class implements both interfaces, but does not have an Add method that takes a single parameter, so it fails.

    More to the point, the xml serialization code explicitly disallows classes that implement IDictionary (from Reflector, looking at TypeScope.GetDefaultIndexer):

     if (typeof(IDictionary).IsAssignableFrom(type)) {
            if (memberInfo == null) {
                throw new NotSupportedException(...);
            }
            throw new NotSupportedException(...);
     }
    

    So you can’t even make the work by subclassing Dictionary<> and adding the required Add overload.

    The article Generic Dictionaries vs. the XmlSerializer describes one workaround:

    1. First, decorate the Dictionary property with
      an XmlIgnore attribute. This tells the
      XmlSerializer to ignore that property,
      eliminating the reflection error.
    2. Create a new public property that takes and returns an array of
      DictionaryEntry objects. I tend to
      name these specially
      (_x_DictionaryName) so that it is
      clear one should not generally use
      this property.
    3. Serialize away.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to deserialize this XML into an object marked with the DataContract
I want to deserialize xml into an object on the android platform.Can you tell
Possible Duplicate: Deserializing JSON into an object with Json.NET Any ideas of how to
Is it possible to deserialize an NSString of JSON into objects via RestKit? I
Is it possible to deserialize an XML file to a class in Flex without
I have the following xml that I need to deserialise to an object: <animals>
With Jackson json library, it is possible to deserialize object through the use of
Given the following XML: <?xml version=1.0?> <user_list> <user> <id>1</id> <name>Joe</name> </user> <user> <id>2</id> <name>John</name>
I have something like the following xml that I need to deserialize (note I
I'd like to know if it is possible to deserialize a char from xml

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.