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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:19:04+00:00 2026-06-12T21:19:04+00:00

i have a class and wish to serialize it into xml. the class contains

  • 0

i have a class and wish to serialize it into xml.
the class contains a dictionary.. switched it to a serializeable version (with writexml / readxml).

the problem is that when the dictionary parameter gets serialized.. it wraps the dictionary elements with a parent element “Attributes” and i dont want that.

Example:

public class Product
{
    public String Identifier{ get; set; }
    [XmlElement]
    public SerializableDictionary<string,string> Attributes { get; set; } //custom serializer
}

This Product class gets put in a List structure, then the whole thing serialized, resulting to:

<Products>
<Product>
     <Identifier>12345</Identifier>
     <Attributes>
          <key1> value 1</key1>
          <key2> value 2</key2>
     </Attributes>
</Product>
</Products>

Id like to not have the node wrapper.

Im using a Serialized Dictionary class that goes around, but through its WriteXml im only able to influence the key value pairs.. not the parent element.

Any self sufficient examples that i could plug to say linqpad would be great..
Heres a short version of the serializable dictionary..

   [XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
    #region IXmlSerializable Members
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        //not including for the sake of brevity
    }
    public void WriteXml(System.Xml.XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
        foreach (TKey key in this.Keys)
        {
            writer.WriteStartElement(key.ToString());
            TValue value = this[key];
            if (value == null)
                writer.WriteValue(String.Empty); //render empty ones.
            else
                writer.WriteValue(value.ToString());
            writer.WriteEndElement();
        }
    }
    #endregion
}
  • 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-12T21:19:05+00:00Added an answer on June 12, 2026 at 9:19 pm

    If you don’t want the <Attributes> element, you need to move the WriteXml method to the Product-class.

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteElementString("Identifier", Identifier.ToString("d"));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
        foreach (TKey key in Attributes.Keys)
        {
            writer.WriteStartElement(key.ToString());
            TValue value = Attributes[key];
            if (value == null)
                writer.WriteValue(String.Empty); //render empty ones.
            else
                writer.WriteValue(value.ToString());
            writer.WriteEndElement();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I wish to do is have a class that contains a map of
I have a class that includes a std::list and wish to provide public begin()
I have a class containing an array I wish to serialize with XmlSerializer :
I have a custom class that I wish to save and load. The class
I have some objects that I wish to cache on disk. I use serialize()
Here is the domain that I wish to have: public class Person { public
I have a class that I wish to test via SimpleXMLRPCServer in python. The
I have a class with an attribute I wish to turn into a property
I have a class that I wish to expose as a remote service using
I have written a base class from which I wish to derive several child

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.