从 BindingList派生的类的公共字段/属性 不会序列化
我正在尝试序列化一个派生自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里简短的回答是,.NET 通常期望某个东西成为集合异或具有属性。 这体现在几个地方:
在 xml 序列化的情况下,如果您认为它可能只是一个
SomeType[]
在客户端...额外的数据会去哪里?常见的解决方案是封装一个集合 - 即
通常我不会在集合属性上设置
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:
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
Normally I wouldn't have a
set
on a collection property, butXmlSerializer
demands it...这是 XML 序列化和从集合继承的已知问题。
您可以在此处阅读有关此内容的更多信息: http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/0d94c4f8-767a-4d0f-8c95-f4797cd0ab8e
您可以尝试这样的操作:
这意味着您可以添加通过使用 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 :
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.
XML 序列化以特定方式处理集合,并且从不序列化集合的字段或属性,而仅序列化项目。
您可以:
XML serialization handles collections in a specific way, and never serializes the fields or properties of the collection, only the items.
You could either :