序列化时是否可以避免列表属性标签?
//[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>
Adding:
[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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)