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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:30:32+00:00 2026-05-24T09:30:32+00:00

Is there a way to cause XmlSerializer to serialize primitive class members (e.g. string

  • 0

Is there a way to cause XmlSerializer to serialize primitive class members (e.g. string properties) as XML attributes, not as XML elements, without having to write [XmlAttribute] in front of each property declaration?
I.e. is there a global switch that tells XmlSerializer to serialize all primitive class members as XML attributes?

Assume that we have the following class:

public class Person
{
    public string FirstName
    {
       ...
    }

    public string LastName
    {
       ...
    }
}

Then XmlSerializer generates this code by default:

<Person>
    <FirstName>John</FirstName>
    <LastName>Doe</LastName>
</Person>

What I want, however, is this code:

<Person FirstName="John" LastName="Doe"/>

Again: I want to do that without [XmlAttribute] (or without XmlAttributeOverrides, which would be even more work).

One possible solution would be to use a generic postprocessing step that applies an XSLT transform to convert elements to attributes. But I wonder whether there is a simpler solution.

  • 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-24T09:30:35+00:00Added an answer on May 24, 2026 at 9:30 am

    One way to achieve this is to implement the serialization logic in a base class that implements the IXmlSerializable interface. The classes that are to be serialized to XML, would then have to derive from this base class in order to get the functionality.

    Here’s an example

    public class XmlSerializableEntity : IXmlSerializable
    {
        public XmlSchema GetSchema()
        {
            // Implementation omitted for clarity
        }
    
        public void ReadXml(XmlReader reader)
        {
            // Implementation omitted for clarity
        }
    
        public void WriteXml(XmlWriter writer)
        {
            var properties = from property in this.GetType().GetProperties()
                             where property.PropertyType.IsPrimitive ||
                                   property.PropertyType == typeof(string)
                             select property;
    
            foreach (var property in properties)
            {
                var name = property.Name;
                var value = property.GetValue(this, null).ToString();
                writer.WriteAttributeString(name, value);
            }
        }
    }
    

    Here we are using Reflection to get a list of properties from the current object, whose type is a primitive or a String. These properties are then written to the XML output as attributes using the provided XmlWriter object.

    The classes to be serialized would simply have to inherit from XmlSerializableEntity to automatically get this behavior:

    [Serializable]
    public class Foo : XmlSerializableEntity
    {
        public int Bar { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to cause a page to redirect without having to use
Is there a way to tell subversion update/merge unless it would cause a conflict?
Is there a way to fall through multiple case statements without stating case value:
Is there a way to use the HAVING clause in some other way without
Is there any way to cause an app to quit instead of going to
Is there any way that i can cause mysql to be case sensitive whe
Consider a simple case public class Test { public String myString; } Is there
Is there any way to cause a compilation error if an ASP.NET control declares
Is there a way to cause hudson to report a build as failed, rather
is there any way to do this on a class and still retain 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.