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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:55:44+00:00 2026-05-22T21:55:44+00:00

How can I change XML-element name for field inherited from base class while doing

  • 0

How can I change XML-element name for field inherited from base class while doing serialization?

For example I have next base class:

public class One
{
    public int OneField;
}

Serialization code:

static void Main()
{
    One test = new One { OneField = 1 };
    var serializer = new XmlSerializer(typeof (One));
    TextWriter writer = new StreamWriter("Output.xml");
    serializer.Serialize(writer, test);
    writer.Close();
}

I get what I need:

<?xml version="1.0" encoding="utf-8"?>
<One xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <OneField>1</OneField>
</One>

Now I have created new class inherited from A with additional field and custom XML element name for it:

public class Two : One
{
    [XmlElement("SecondField")]
    public int TwoField;
}

Serialization code:

static void Main()
{
    Two test = new Two { OneField = 1, TwoField = 2 };

    var serializer = new XmlSerializer(typeof (Two));
    TextWriter writer = new StreamWriter("Output.xml");
    serializer.Serialize(writer, test);
    writer.Close();
}

As a result I get next output:

<?xml version="1.0" encoding="utf-8"?>
<Two xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <OneField>1</OneField>
  <SecondField>2</SecondField>
</Two>

The problem is that I want to change OneField in this output to FirstField without touching base class code (because I will use it too and the names must be original). How can I accomplish this?

Thank you.

  • 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-22T21:55:45+00:00Added an answer on May 22, 2026 at 9:55 pm

    Try this:

    public class Two : One
    {
        private static XmlAttributeOverrides xmlOverrides;
        public static XmlAttributeOverrides XmlOverrides
        {
            get
            {
                if (xmlOverrides == null)
                {
                    xmlOverrides = new XmlAttributeOverrides();
                    var attr = new XmlAttributes();
                    attr.XmlElements.Add(new XmlElementAttribute("FirstField"));
                    xmlOverrides.Add(typeof(One), "OneField", attr);
                }
                return xmlOverrides;
            }
        }
    
        [XmlElement("SecondField")]
        public string TwoField;
    
    }
    

    And your serializer init is a lot easier:

     var xmls = new System.Xml.Serialization.XmlSerializer(typeof(Two), Two.XmlOverrides);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML part which schema I can't change. NUMBER, REGION, MENTION,
Here is an example bit from the xml file: <array> <dict> <key>Name</key> <string>Joe Smith</string>
Can someone tell me how i can change the .xml file that a flash
Suppose I have this XML document... <root> <str name=My node's attribute>My string's value</str> </root>
El Padrino showed a solution: How to change XML Attribute where an xml element
You can change the connection string at run-time like this. You make the connection
I understand how I can change the dns settings for my domains by editing
Is there an app that can change the order of images inside an icon?
Is there any way that I can change how a Literal of a code
I can set data in JTable constructor, and then user can change this data

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.