【问题标题】:Omit Properties from WebControl Serialization从 WebControl 序列化中省略属性
【发布时间】:2010-06-14 18:25:35
【问题描述】:

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

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

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

我一直在尝试为我的控件创建一个公共基类,该基类继承自 WebControl,并使用“xxxSpecified”技巧选择性地选择不序列化某些属性。

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

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

工作,但在序列化过程中从未调用过。

在要序列化的类和基类中我也试过了。

由于类本身可能会改变,我不希望必须创建自定义序列化程序。有什么想法吗?

编辑

我已经在使用XmlAttributeOverrides,但显然不正确。我没有意识到你不能指定一个基类。我调整了我的例程,但它仍然无法正常工作。以下是我尝试过的更多细节。

我有一个名为 Activity 的 WebControl,它有一个 ContainerPanel(继承 Panel),其中包含多个 SerializePanel 类型的控件(也继承 Panel)。

尝试 1 我将 [XmlIgnore] 属性添加到 SerializePanel 的新属性没有效果。该属性仍包含在序列化中。

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

尝试 2 我也试过SerializePanel声明中的*Specified,但是被忽略了

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

尝试 3 然后在序列化程序中,我传递了这里创建的覆盖:

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 类型的属性是必要的,以便能够序列化 Control。

生成的 XML sn-p 是每次尝试的结果

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

【问题讨论】:

    标签: c# xml-serialization web-controls


    【解决方案1】:

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

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

    另外,XmlIgnore 属性是不必要的,因为只读属性没有被序列化(无论如何它是你代码中的一个方法)

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


    编辑:

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

    但是,您可以使用另一个选项:XmlAttributeOverrides 类。这允许您在不更改类代码的情况下自定义序列化:

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

    这种方法可能更好,因为您不需要为控件创建公共基类。此外,您不需要使用除了序列化之外没有其他用途的新公共成员污染您的类

    【讨论】:

    • 非常感谢您的见解。不幸的是,我仍然遇到一些我怀疑与嵌套控件有关的问题,尽管此时这只是一种预感。我想知道你是否可以看看还有什么问题。请参阅问题编辑。
    • @Laramie,您需要为定义属性的类型指定属性。例如,BackColor 是在 WebControl 中定义的,而不是在 Control 中。所以你必须写overrides.Add(typeof(WebControl), strAttr, ignoreAtrs)
    • 这很有意义。适用于所有继承 WebControl 的对象,但我仍然看不到有选择地选择哪些继承 WebControl 的类型应该序列化其属性的方法。如果我在像 SerializePanel 这样的继承类型上创建 public new System.Drawing.Color BackColor 属性并设置 overrides.Add(typeof(SerializePanel), "BackColor", ignoreAtrs) BackColor 仍在序列化中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2021-07-14
    相关资源
    最近更新 更多