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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:00:50+00:00 2026-06-03T08:00:50+00:00

When I add an XmlAttribute/XmlElement to a property in an Interface and 100 other

  • 0

When I add an “XmlAttribute/XmlElement” to a property in an Interface and 100 other classes inherit from this interface, when Serializing the object to XML – why doesn’t the attributename I entered show up in the XML file after serialization?

interface Test
{
    [XmlAttribute("Name")]
    bool PropertyName { get; set; }
}

when saving the file, is shows “PropertyName” and not “Name”.

Is there any way to make it work, so a proeprty with an XmlAttributes added to an interface changes the Value everywhere instead of Value because if a few classes inherit from the same interface it takes alot of time to add all those Attributes to all those classes that inherit it, and it would be easier to change the Name of the Attribute instead of changing a 100 of them.

  • 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-03T08:00:51+00:00Added an answer on June 3, 2026 at 8:00 am

    Deukalin, spent some time and make it work with comprehensive tree of classes and interfaces.

    Here is the working code

        public interface ITestX
        {
            [XmlAttribute("NameX")]
            string PropertyNameX { get; set; }
        }
    
        public interface ITestY
        {
            [XmlAttribute("NameY")]
            string PropertyNameY { get; set; }
        }
    
        public interface ITestZ
        {
            [XmlAttribute("NameZ")]
            string PropertyNameZ { get; set; }
        }
    
        public abstract class TestC : ITestZ
        {
            public abstract string PropertyNameZ { get; set; }
        }
    
        public abstract class TestA : TestC, ITestX, ITestY
        {
            public abstract string PropertyNameX { get; set; }
            public abstract string PropertyNameY { get; set; }
        }
    
        public class TestB : TestA
        {
            public override string PropertyNameX { get; set; }
            public override string PropertyNameY  { get; set; }
            public override string PropertyNameZ { get; set; }
        }
    
        public static class ClassHandler
        {
            public static T GetCustomAttribute<T>(this PropertyInfo propertyInfo, bool inherit) where T : Attribute
            {
                object[] attributes = propertyInfo.GetCustomAttributes(typeof(T), inherit);
    
                return attributes == null || attributes.Length == 0 ? null : attributes[0] as T;
            }
    
            public static void GetXmlAttributeOverrides(XmlAttributeOverrides overrides, Type type)
            {
                if (type.BaseType != null)
                {
                    GetXmlAttributeOverrides(overrides, type.BaseType);
                }
    
                foreach (Type derived in type.GetInterfaces())
                {
                    foreach (PropertyInfo propertyInfo in derived.GetProperties())
                    {
                        XmlAttributeAttribute xmlAttributeAttribute = ClassHandler.GetCustomAttribute<XmlAttributeAttribute>(propertyInfo, true) as XmlAttributeAttribute;
    
                        if (xmlAttributeAttribute == null)
                            continue;
    
                        XmlAttributes attr1 = new XmlAttributes();
                        attr1.XmlAttribute = new XmlAttributeAttribute();
                        attr1.XmlAttribute.AttributeName = xmlAttributeAttribute.AttributeName;
                        overrides.Add(type, propertyInfo.Name, attr1);
                    }
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                XmlAttributeOverrides XmlAttributeOverrides = new XmlAttributeOverrides();
                ClassHandler.GetXmlAttributeOverrides(XmlAttributeOverrides, typeof(TestB));
                try
                {
                    TestB xtest = new TestB() { PropertyNameX = "RajX", PropertyNameY = "RajY", PropertyNameZ = "RajZ" };
    
                    StringBuilder xmlString = new StringBuilder();
                    using (XmlWriter xtw = XmlTextWriter.Create(xmlString))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(TestB), XmlAttributeOverrides);
                        serializer.Serialize(xtw, xtest);
    
                        xtw.Flush();
                    }
    
                    Console.WriteLine(xmlString.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
    }
    

    Below is the output of sample above

    <?xml version="1.0" encoding="utf-16"?><TestB xmlns:xsi="http://www.w3.org/2001/
    XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" NameZ="RajZ" Na
    meX="RajX" NameY="RajY" />
    Press any key to continue . . .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When serializing a custom generic collection to Xml how do I add an attribute
I have an xml in XDocument object (LINQ to XML). I need to add
I add my page top link button like this: <a name=top></a> . . .
I add this class to library/My/Validate/PasswordConfirmation.php <?php require_once 'Zend/Validate/Abstract.php'; class My_Validate_PasswordConfirmation extends Zend_Validate_Abstract {
I add this code in my activity public boolean onKeyDown(int keyCode, KeyEvent event) {
I add local maven repository, I add the local dependency in my project. this
I add this tag to my asp.net page (aspx): <obout:Calendar runat=server ShowTimeSelector=true DateFormat=MM/dd/yyyy hh:mm:ss
I am trying to add document to the index using c# (xml) but I
This is the scenario: I have nested classes and need to serialize then in
i have the following classes which is marshalled as an XML <?xml version=1.0 encoding=UTF-8

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.