本文介绍了公共字段/距离的BindingList的&lt类的属性; T>不会连载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图序列化,从的BindingList(楼)派生的类,其中地板是一个简单的类,只包含一个属性的 Floor.Height

I'm trying to serialize a class that derives from BindingList(Floor), where Floor is a simple class that only contains a property Floor.Height

下面是我班的简化版本,

Here's a simplified version of my class

[Serializable]
[XmlRoot(ElementName = "CustomBindingList")]
public class CustomBindingList:BindingList<Floor>
{
    [XmlAttribute("publicField")]
    public string publicField;
    private string privateField;

    [XmlAttribute("PublicProperty")]
    public string PublicProperty
    {
        get { return privateField; }
        set { privateField = value; }
    }
}

我会用下面的code序列CustomBindingList的一个实例。

I'll serialize an instance of CustomBindingList using the following code.

XmlSerializer ser = new XmlSerializer(typeof(CustomBindingList));
StringWriter sw = new StringWriter();

CustomBindingList cLIst = new CustomBindingList();

Floor fl;

fl = new Floor();
fl.Height = 10;
cLIst.Add(fl);

fl = new Floor();
fl.Height = 10;    
cLIst.Add(fl);

fl = new Floor();
fl.Height = 10;
cLIst.Add(fl);

ser.Serialize(sw, cLIst);

string testString = sw.ToString();

然而,的TestString 上面两端越来越设置为以下XML:

Yet testString above ends getting set to the following XML:

<CustomBindingList xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
    <Floor Height="10" />
    <Floor Height="10" />
    <Floor Height="10" />
</CustomBindingList>"

我如何得到publicField或publicProperty序列化呢?

How do I get "publicField" or "publicProperty to serialize as well?

推荐答案

XML序列化处理以特定的方式收藏,从不序列化字段或集合的性质,只有项目。

XML serialization handles collections in a specific way, and never serializes the fields or properties of the collection, only the items.

您既可以:


  • 实施IXmlSerializable的生成和解析自己的XML(但它是一个大量的工作)

  • 包装您的BindingList另一个类,在其中声明你的自定义字段(如speps建议)

这篇关于公共字段/距离的BindingList的&lt类的属性; T&GT;不会连载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:43