从 BindingList派生的类的公共字段/属性 不会序列化

发布于 2024-07-29 20:21:02 字数 1423 浏览 11 评论 0原文

我正在尝试序列化一个派生自 BindingList(Floor) 的类,其中 Floor 是一个简单的类,仅包含属性 Floor.Height >

这是我的类的简化版本,

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

我将使用以下代码序列化 CustomBindingList 的实例。

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:

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

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柳絮泡泡 2024-08-05 20:21:03

这里简短的回答是,.NET 通常期望某个东西成为集合异或具有属性。 这体现在几个地方:

  • xml 序列化; 集合的属性不是序列化的
  • 数据绑定; 您无法将数据绑定到集合上的属性,因为它隐式地将您带到第一个项目。

在 xml 序列化的情况下,如果您认为它可能只是一个 SomeType[] 在客户端...额外的数据会去哪里?

常见的解决方案是封装一个集合 - 即

public class MyType : List<MyItemType> // or BindingList<...>
{
    public string Name {get;set;}
}

public class MyType
{
    public string Name {get;set;}
    public List<MyItemType> Items {get;set;} // or BindingList<...>
}

通常我不会在集合属性上设置set,但是XmlSerializer需要它...

The short answer here is that .NET generally expects something to be a collection xor to have properties. This manifests in a couple of places:

  • xml serialization; properties of collections aren't serialized
  • data-binding; you can't data-bind to properties on collections, as it implicitly takes you to the first item instead

In the case of xml serialization, it makes sense if you consider that it might just be a SomeType[] at the client... where would the extra data go?

The common solution is to encapsulate a collection - i.e. rather than

public class MyType : List<MyItemType> // or BindingList<...>
{
    public string Name {get;set;}
}

public class MyType
{
    public string Name {get;set;}
    public List<MyItemType> Items {get;set;} // or BindingList<...>
}

Normally I wouldn't have a set on a collection property, but XmlSerializer demands it...

弄潮 2024-08-05 20:21:03

这是 XML 序列化和从集合继承的已知问题。

您可以在此处阅读有关此内容的更多信息: http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/0d94c4f8-767a-4d0f-8c95-f4797cd0ab8e

您可以尝试这样的操作:

[Serializable]
[XmlRoot]
public class CustomBindingList
{
    [XmlAttribute]
    public string publicField;
    private string privateField;

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

    [XmlElement]
    public BindingList<Floor> Floors = new BindingList<Floor>();
}

这意味着您可以添加通过使用 Floors.Add 来添加楼层,你会得到你想要的结果,我希望,但是,我没有尝试过。 请记住,使用属性是 XML 序列化的关键。

This is known issue with XML serialization and inheriting from collections.

You can read more info on this here : http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/0d94c4f8-767a-4d0f-8c95-f4797cd0ab8e

You could try something like this :

[Serializable]
[XmlRoot]
public class CustomBindingList
{
    [XmlAttribute]
    public string publicField;
    private string privateField;

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

    [XmlElement]
    public BindingList<Floor> Floors = new BindingList<Floor>();
}

This means you can add floors by using Floors.Add and you will get the result you want, I hope, however, I didn't try it. Keep in mind that playing around with attributes is the key to XML serialization.

ゃ懵逼小萝莉 2024-08-05 20:21:03

XML 序列化以特定方式处理集合,并且从不序列化集合的字段或属性,而仅序列化项目。

您可以:

  • 实现 IXmlSerialized 自己生成和解析 XML(但这需要大量工作)
  • 将 BindingList 包装在另一个类中,在其中声明自定义字段(如 speps 所建议)

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

You could either :

  • implement IXmlSerializable to generate and parse the XML yourself (but it's a lot of work)
  • wrap your BindingList in another class, in which you declare your custom fields (as suggested by speps)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文