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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:23:01+00:00 2026-06-11T20:23:01+00:00

Is there a way to serialize key/value pairs (preferably strongly typed, but possibly also

  • 0

Is there a way to serialize key/value pairs (preferably strongly typed, but possibly also sourced from a Dictionary) into the desired format below?

public List<Identifier> Identifiers = new List<Identifiers>();

public class Identifier
{
    public string Name { get; set; }
    public string Description { get; set; }
}

This normally serializes to the following:

<Identifiers>
  <Identifier>
    <Name>somename</Name>
    <Description>somedescription</Description>
  </Identifier>
  <Identifier>
    ...
  </Identifier>
</Identifiers>

The other possible approach we were thinking about is to use a hashtable/dictionary:

public Dictionary<string, string> Identifiers = new Dictionary<string,string>
{
    { "somename", "somedescription"},
    { "anothername", "anotherdescription" }
};

But this will either require a custom serialized Dictionary, or a custom XmlWriter.

The output we would like to achieve is:

<Identifiers>
  <somename>somedescription</somename>
  <anothername>anotherdescription</anothername>
</Identifiers>

So we’re looking for code samples as to how best approach this to get the output we desire.

Edit: Maybe I should explain better. We already know how to serialize objects. What we are looking for is the answer to a particular type of serialization… I’ll expand the question above

  • 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-11T20:23:02+00:00Added an answer on June 11, 2026 at 8:23 pm

    This is hard to answer as you don’t really clarify what ‘best’ means to you.

    Fastest would probably be the raw write out as strings:

    var sb = new StringBuilder();
    sb.Append("<identifiers>");
    foreach(var pair in identifiers)
    {
        sb.AppendFormat("<{0}>{1}</{0}>", pair.Key, pair.Value);
    }
    sb.Append("</identifiers>");
    

    Obviously that’s not handling any escaping to XML, but then that might not be a problem, it depends entirely on the contents of your dictionary.

    What about fewest lines of code? If that’s your requirement then L.B.’s Linq to XML answer’s probably best.

    What about smallest memory footprint? There I’d look at dropping the Dictionary and creating your own serialisable class that drops the hash overhead and collection functionality in favour of just storing name and value. That might be fastest too.

    If code simplicity is your requirement then how about using dynamic or anonymous types instead of Dictionary?

    var anonType = new
    { 
        somename = "somedescription",
        anothername = "anotherdescription" 
    }
    
    // Strongly typed at compile time
    anonType.anothername = "new value";
    

    That way you’re not dealing with ‘magic strings’ for the names of properties in your collection – it will be strongly typed in your code (if that’s important for you).

    However anonymous types don’t have a built in serialiser – you’d have to write something for yourself, use one of the many open source alternatives or even use the XmlMediaTypeFormatter.

    There are loads of ways to do this, which one is best depends on how you’re going to use it.

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

Sidebar

Related Questions

Is there a way in Python to serialize a dictionary that is using a
Is there a simple way to serialize a single-level structure as a string for
Is there way to get file from windows xp command prompt? I tried to
is there way how to get name ov event from Lambda expression like with
Is there way that I can read the file from remote server using fopen
Short version: Is there a simple way to get the value of a primary
is there a way to serialize all form elements using serialize() function except the
Is there an easy way to serialize data in c++ (either to xml or
I'd like to store a set of key/value pairs in the application settings of
How can dynamic Key-value pairs of objects be stored in app.config in using the

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.