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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:35:54+00:00 2026-06-12T11:35:54+00:00

I have a weird requirement on xml serialization. Refer the following C# code (which

  • 0

I have a weird requirement on xml serialization.

Refer the following C# code (which cannot be compiled due to the variable ‘rootName’ is out of scope). My intention is to make my class GeneralData be ‘general’. Which means this class can be serialized to different XML strings with different root element according to the input parameter for the class constructor.

[XmlRoot(ElementName = rootName)]
public class GeneralData : Dictionary<String, Object>, IXmlSerializable
{
    public string rootName;
    public GeneralData(string rootName)
    {
        this.rootName = rootName;
    }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        throw new NotImplementedException();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        foreach (var key in Keys)
        {
            var value = base[key];
            writer.WriteElementString(key, value.ToString());
        }
    }
}

Anyone can help me to accomplish the task? Maybe in a totally different way?
Thanks in advance!!

  • 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-12T11:35:55+00:00Added an answer on June 12, 2026 at 11:35 am

    IXmlSerializable does not get to control the root element. So no, you can’t really do that. The closest you could do would be to use new XmlSerializer(...) with the overload that lets you specify the root name at runtime (into the constructor), but you should be cautious: the non-trivial constructors of XmlSerializer do not use the inbuilt serializer-cache, meaning: you can end up creating a new assembly per new XmlSerializer(...), and assemblies are never unloaded. This can lead to memory leak issues if you don’t add your own caching layer for the serializer instances.

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

Sidebar

Related Questions

I have the following weird requirement. I am given: A list of some method
I have a weird requirement which I need to use inside my Stored Procedure
I have a weird question. After profiling my code several times with NYTProf. I
I have another weird problem which I have not been able to solve in
I have a kind of funny and weird requirement this time. I have my
I have this weird requirement to access scanner, barcode reader, camera etc. from an
I have a weird requirement for setting up a password rule.. :| My requirement
I have a very weird problem with core text, which sometimes randomly and sometimes
This is a weird requirement I have. I know even my question is quite
I have weird problem that I cant figure it out. I've made mobile app

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.