从WebControl序列化中忽略属性 [英] Omit Properties from WebControl Serialization

查看:110
本文介绍了从WebControl序列化中忽略属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须序列化从WebControl继承的几个对象以进行数据库存储.这些包括一些我不想从序列化中忽略的不必要的属性(对我而言).例如BackColor,BorderColor等.

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.

这是从WebControl继承的我的一个控件的XML序列化示例.

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>

我一直在尝试为从WebControl继承并使用"xxx

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.

例如,忽略一个空的BorderColor属性,我希望

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?

修改:

我已经在使用XmlAttributeOverrides了,尽管显然是错误的.我没有意识到您无法指定基类.我调整了例程,但仍然无法正常工作.这是我尝试过的事情的更多详细信息.

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.

我有一个名为Activity的WebControl,它具有一个ContainerPanel(继承于Panel),其中包含几个SerializePanel类型的控件(也继承了Panel).

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

尝试1 我将[XmlIgnore]属性添加到SerializePanel的新属性中没有任何作用.该属性仍包含在序列化中.

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 { }}

尝试2 我也尝试了* Specialize在SerializePanel的声明中,但被忽略了

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

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

尝试3 然后在序列化器中,我传递了在此处创建的替代项:

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);
}

注意:为使控件序列化,必须添加System.Web.UI.Control类型的属性.

生成的XML代码段是每次尝试的

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>
...

推荐答案

XXXSpecified必须是属性,而不是方法:

XXXSpecified has to be a property, not a method :

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

此外,XmlIgnore属性不是必需的,因为只读属性未序列化(无论如何,这是您代码中的方法)

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

或者,您可以使用ShouldSerializeXXX 方法代替XXXSpecified属性

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

根据马克的回答,XXXSpecified技巧在这里不起作用...

According to Marc's answer, the XXXSpecified trick won't work here...

但是,您还有另一种选择: XmlAttributeOverrides 类.这使您可以自定义序列化而无需更改类的代码:

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

这篇关于从WebControl序列化中忽略属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆