如何阻止XmlSerializer发出空标签? [英] How do I stop an empty tag from being emitted by XmlSerializer?

查看:248
本文介绍了如何阻止XmlSerializer发出空标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的对象

public class UserObj
{
    public string First {get; set;}
    public string Last  {get; set;}
    public addr Address {get; set;}

}

public class addr
{
    public street {get; set;}
    public town   {get; set;}
}

现在当我在上面使用XmlSerializer且街道和城镇为空时我在XML输出中得到了这个,

Now when I use XmlSerializer on it and street and town are empty I get this in the XML output,

 <Address />

是否有一种方法不输出该空标签?

Is there a way not to output this empty tag?

谢谢

推荐答案

您可以实现 IXmlSerializable 并自行实现序列化例程。这样,您可以避免该元素。

You may implement IXmlSerializable and implement the serialization routine on your own. This way, you can avoid the element.

此处的示例: http://paltman.com/2006/jul/03/ixmlserializable-a-persistable-example/

这篇关于如何阻止XmlSerializer发出空标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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