序列化时是否可以避免列表属性标签?
//[Serializable()] - removed,unnecessary public class Foo { protected List<FooBar> fooBars = new List<FooBar>(); public virtual List<FooBar> FooBars { get { return fooBars; } set { fooBars = value; } } } // [Serializable()] - removed,unnecessary public class FooBar { public int MyProperty { get; set; } }
序列化Foo给出(注释除外):
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <FooBars> <!-- Unwanted tag --> <FooBar> <MyProperty>7</MyProperty> </FooBar> <FooBar> <MyProperty>9</MyProperty> </FooBar> </FooBars> </Foo>
想输出:
<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <FooBar> <MyProperty>7</MyProperty> </FooBar> <FooBar> <MyProperty>9</MyProperty> </FooBar>
解决方法
添加:
[System.Xml.Serialization.XmlElement("FooBar")] public virtual List<FooBar> FooBars { get { return fooBars; } set { fooBars = value; } }
结果是
<FooMain xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http:/ /www.w3.org/2001/XMLSchema"> <FooBar> <MyProperty>7</MyProperty> </FooBar> <FooBar> <MyProperty>76</MyProperty> </FooBar> <FooBar> <MyProperty>67</MyProperty> </FooBar> </FooMain>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。