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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:31:31+00:00 2026-06-08T18:31:31+00:00

I implement IXmlSerializable for the type below which encodes a RGB color value as

  • 0

I implement IXmlSerializable for the type below which encodes a RGB color value as a single string:

public class SerializableColor : IXmlSerializable
{
    public int R { get; set; }
    public int G { get; set; }
    public int B { get; set; }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        var data = reader.ReadString();
        reader.ReadEndElement();
        var split = data.Split(' ');
        R = int.Parse(split[0]);
        G = int.Parse(split[1]);
        B = int.Parse(split[2]);
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteString(R + " " + G + " " + B);
    }
}

Since it’s a single string, I wanted to store it as an attribute to save space. But as soon as I add the [XmlAttribute] to my property, I get the following exception:

{“Cannot serialize member ‘Color’ of type SerializableColor. XmlAttribute/XmlText cannot be used to encode types implementing IXmlSerializable.”}

Is there a way to make it work as an attribute too?

  • 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-08T18:31:32+00:00Added an answer on June 8, 2026 at 6:31 pm

    The error means exactly what it says. You cannot use these XML serialization attributes when IXmlSerializable is implemented because IXmlSerializable expects the XML serialization to be completely customized. You can do this if you want to make the class serializable with XmlSerializer using the attributes.

    [XmlRoot("SerializableColor")]
    public class SerializableColor
    {
        [XmlAttribute("R")]
        public int R { get; set; }
        [XmlAttribute("G")]
        public int R { get; set; }
        [XmlAttribute("B")]
        public int B { get; set; }    
    }
    

    Also, for your implementation of XmlSerializable:

        public void ReadXml(XmlReader reader)
        {
            string data = null;
    
            reader.MoveToAttribute("Color");
            if (reader.ReadAttributeValue())
            {
                data = reader.Value;
            }
            reader.ReadEndElement();
    
            var split = data.Split(' ');
            R = int.Parse(split[0]);
            G = int.Parse(split[1]);
            B = int.Parse(split[2]);
        }
    
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteAttributeString("Color", R + " " + G + " " + B);
        }
    

    If, on the other hand, all you are looking to be able to do is have a short string represenation of a color that is reversible, have a look at the ColorTranslator Class. In particular, see the FromHtml and ToHtml methods.

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

Sidebar

Related Questions

My Class implements IXmlSerializable and has a property like this: public List<KeyValuePair<int, bool>> exportList
Is there any way to customize or control which type of collection class types
I have a class in .NET that implements IXmlSerializable. I want to serialize its
Implement an algorithm to determine if a string has all unique characters. What if
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
Is it possible to implement IXmlSerializable and in my XML file capture an object
I have a class containing a SortedList<string, Data> as private field, where Data is
I am implementing IXmlSerializable in an immutable class. To keep the class immutable I
I implement a Singleton using the enum approach: public enum Singleton { INSTANCE; public
Once a programmer decides to implement IXmlSerializable , what are the rules and best

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.