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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:10:29+00:00 2026-05-15T10:10:29+00:00

I have to serialize several objects inheriting from WebControl for database storage. These include

  • 0

I have to serialize several objects inheriting from WebControl for database storage. These include several unecessary (to me) properties that I would prefer to omit from serialization. For example BackColor, BorderColor, etc.

Here is an example of an XML serialization of one of my controls inheriting from WebControl.

<Control xsi:type="SerializePanel">
        <ID>grCont</ID>
        <Controls />
        <BackColor />
        <BorderColor />
        <BorderWidth />
        <CssClass>grActVid bwText</CssClass>
        <ForeColor />
        <Height />
        <Width />
        ...
      </Control>

I have been trying to create a common base class for my controls that inherits from WebControl and uses the “xxxSpecified” trick to selectively choose not to serialize certain properties.

For example to ignore an empty BorderColor property, I’d expect

[XmlIgnore]    
public bool BorderColorSpecified()
{
    return !base.BorderColor.IsEmpty;
}

to work, but it’s never called during serialization.

I’ve also tried it in the class to be serialized as well as the base class.

Since the classes themselves might change, I’d prefer not to have to create a custom serializer. Any ideas?

Edit:

I was already using XmlAttributeOverrides though apparently incorrectly. I didn’t realize you couldn’t specify a base class. I tweaked my routine, but it is still not working. Here are some more details of the things I’ve tried.

I have WebControl named Activity, that has a ContainerPanel (inherits Panel) which contains several controls of type SerializePanel (also inherits Panel).

Attempt 1
I added the [XmlIgnore] attributes to new properties of SerializePanel has no effect. The property is still included in serialization.

//This is ignored
[XmlIgnore]
public new System.Drawing.Color  BackColor{
get {  return base.BackColor;   }
set { }}

Attempt 2
I also tried the *Specified in the declaration of SerializePanel, but it was ignored

public bool BackColorSpecified
    {
        get { return !base.BackColor.IsEmpty; }
    }

Attempt 3
Then in the serializer I passed the overrides created here:

XmlAttributeOverrides overrides = new XmlAttributeOverrides();

string[] serPAnelProps = { "BackColor", "BorderColor", "ForeColor", "Site", "Page", "Parent", "TemplateControl", "AppRelativeTemplateSourceDirectory" };
foreach (string strAttr in serPAnelProps)
{
    XmlAttributes ignoreAtrs = new XmlAttributes();
    ignoreAtrs.XmlIgnore = true;
    overrides.Add(typeof(SerializePanel), strAttr, ignoreAtrs);
}

string[] ignoreProps = { "Site", "Page", "Parent", "TemplateControl", "AppRelativeTemplateSourceDirectory" };
foreach (string strAttr in ignoreProps)
{
    XmlAttributes ignoreAtrs = new XmlAttributes();
    ignoreAtrs.XmlIgnore = true;
    overrides.Add(typeof(System.Web.UI.Control), strAttr, ignoreAtrs);
}

Note: The attribute additions to the System.Web.UI.Control type are necessary in order to be able to serialize a Control.

The resulting XML snippet is for each attempt was

<Activity....>
...
<ContainerPanel>
      <ID>actPnl_grAct207_0</ID> 
    - <Controls>
    - <Control xsi:type="SerializePanel">
      <ID>grCont</ID> 
      <Controls /> 
      <BackColor /> 
      <BorderColor /> 
      <BorderWidth /> 
      <CssClass>grActVid</CssClass> 
      <ForeColor /> 
      <Height /> 
      <Width /> 
      <WidthUnitType>Pixel</WidthUnitType> 
      <HeightUnitType>Pixel</HeightUnitType> 
      <WidthUnit>0</WidthUnit> 
      <HeightUnit>0</HeightUnit> 
      </Control>
...
  • 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-15T10:10:30+00:00Added an answer on May 15, 2026 at 10:10 am

    XXXSpecified has to be a property, not a method :

    public bool BorderColorSpecified
    {
        get { return !base.BorderColor.IsEmpty; }
    }
    

    Also, the XmlIgnore attribute is unnecessary, since read-only property are not serialized (and anyway it was a method in your code)

    Alternatively, you can use a ShouldSerializeXXX method instead of a XXXSpecified property


    EDIT:

    According to Marc’s answer, the XXXSpecified trick won’t work here…

    However, there’s another option available to you : the XmlAttributeOverrides class. This allows you to customize the serialization without changing the code of the class :

    XmlAttributeOverrides overrides = new XmlAttributeOverrides();
    
    // Ignore the BackColor property
    XmlAttributes attributesBackColor = new XmlAttributes();
    attributesBackColor.XmlIgnore = true;
    overrides.Add(typeof(WebControl), "BackColor", attributesBackColor);
    
    // do the same for other properties to ignore...
    
    XmlSerializer xs = new XmlSerializer(typeof(YourControl), overrides);
    

    This approach is probably better, because you don’t need to create a common base class for your controls. Also, you don’t need to pollute your classes with new public members that serve no purpose except serialization

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

Sidebar

Ask A Question

Stats

  • Questions 470k
  • Answers 470k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You're looking for the null coalesce operator. It allows you… May 16, 2026 at 3:06 am
  • Editorial Team
    Editorial Team added an answer This operation is called GROUP_CONCAT in MySQL but SQL Server… May 16, 2026 at 3:06 am
  • Editorial Team
    Editorial Team added an answer I think the best way of explaining is to walk… May 16, 2026 at 3:06 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.