如何阻止 XmlSerializer 发出空标记?

发布于 2024-09-04 02:37:23 字数 401 浏览 5 评论 0原文

我有一个像这样的对象,

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 输出中得到这个,

 <Address />

有没有办法不输出这个空标签?

谢谢

I have an object like this,

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

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?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏日落 2024-09-11 02:37:23

您可以实现一个 ShouldSerializeAddress 方法来决定是否应序列化 Address 属性:

public bool ShouldSerializeAddress()
{
    return Address != null
        && !String.IsNullOrEmpty(Address.street)
        && !String.IsNullOrEmpty(Address.town);
}

如果该方法存在于此签名,则序列化程序将在序列化该属性之前调用它。

或者,您可以实现具有相同作用的 AddressSpecified 属性:

public bool AddressSpecified
{
    get
    {
        return Address != null
            && !String.IsNullOrEmpty(Address.street)
            && !String.IsNullOrEmpty(Address.town);
    }
}

You can implement a ShouldSerializeAddress method to decide whether or not the Address property should be serialized :

public bool ShouldSerializeAddress()
{
    return Address != null
        && !String.IsNullOrEmpty(Address.street)
        && !String.IsNullOrEmpty(Address.town);
}

If the method exists with this signature, the serializer will call it before serializing the property.

Alternatively, you can implement an AddressSpecified property which has the same role :

public bool AddressSpecified
{
    get
    {
        return Address != null
            && !String.IsNullOrEmpty(Address.street)
            && !String.IsNullOrEmpty(Address.town);
    }
}
掀纱窥君容 2024-09-11 02:37:23

您可以通过向属性添加 DefaultValue 特性来消除空值。当属性的值与默认值匹配时,它不会被序列化。您将默认值设置为 null,以消除序列化。这是一个例子:


using System.ComponentModel;
public class UserObj
{
    public string First {get; set;}
    public string Last  {get; set;}

    [DefaultValue(null)]
    public addr Address {get; set;}

}

You can eliminate the empty value by adding a DefaultValue attribute to the property. When the value of the property matches the default value, it isn't serialized. You set the default value to null, to eliminate the serialization. Here's an example:


using System.ComponentModel;
public class UserObj
{
    public string First {get; set;}
    public string Last  {get; set;}

    [DefaultValue(null)]
    public addr Address {get; set;}

}
陪我终i 2024-09-11 02:37:23

我认为为地址字段分配空值应该可行。

I think assigning null value to address field should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文