如何打压列表属性的XML标记 [英] How to suppress XML tag for list property

查看:92
本文介绍了如何打压列表属性的XML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能避免列表属性标记序列化时

Is it possible to avoid list property tags when serializing?

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



序列化美孚给出了(除了注解):

Serializing Foo gives (except the comment):

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



结果

Results in

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

这篇关于如何打压列表属性的XML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆