asmx 服务可以不返回任何内容而不返回 null 吗?

发布于 2024-08-06 12:01:01 字数 823 浏览 2 评论 0原文

我有一个正在维护的 WebService,在 .Net 2.0 上运行。它使用原始的“asmx”文件标准来提供一系列Web服务。在这些 Web 服务中,返回的某些对象可能具有大量“空”值。例如:

<user id="1" name="foo" job="null" location="null" audience="null" />

这是一个简单的例子;事实上,我们有更多的“空”值。因为我真的不需要空值,因为我可以很容易地推断它们因不存在而为空,所以我宁愿根本不返回它们。这可以做到吗?如果是这样,怎么办?

编辑以添加类定义:

[Serializable]
public partial class User

    [XmlAttribute("Id")]
    public int Id 
    {
        get { return GetColumnValue<int>("ID"); }

        set { SetColumnValue("ID", value); }

    }



    [XmlAttribute("Username")]
    public string Username 
    {
        get { return GetColumnValue<string>("Username"); }

        set { SetColumnValue("Username", value); }

    }
}

顺便说一句,我想要看到的是:

<user id="1" name="foo" />

I have a WebService I'm maintaining, running on .Net 2.0. It uses the original "asmx" file standard for a series of web services. In these web services, some objects are returned that have potentially a large number of "null" values. For example:

<user id="1" name="foo" job="null" location="null" audience="null" />

This is a simple example; in reality, we have a lot more "null" values. Since I don't really need to have the nulls because I can easily infer that they're null from their non-existence, I'd prefer to not return them at all. Can this be done? If so, how?

Edited to add class definition:

[Serializable]
public partial class User

    [XmlAttribute("Id")]
    public int Id 
    {
        get { return GetColumnValue<int>("ID"); }

        set { SetColumnValue("ID", value); }

    }



    [XmlAttribute("Username")]
    public string Username 
    {
        get { return GetColumnValue<string>("Username"); }

        set { SetColumnValue("Username", value); }

    }
}

By the way, what I'm aiming to see is:

<user id="1" name="foo" />

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

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

发布评论

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

评论(4

小嗲 2024-08-13 12:01:01

XmlElementAttribute.IsNullable 属性

如果 IsNullable 属性为 false,则不会为已设置为 null 引用(在 Visual Basic 中为 Nothing)的类成员生成任何 XML 元素。

public class MyClass
{
   [XmlElement(IsNullable = false)]
   public string Group;
}

XmlElementAttribute.IsNullable Property

If the IsNullable property is false, no XML element is generated for class members that have been set to a null reference (Nothing in Visual Basic).

public class MyClass
{
   [XmlElement(IsNullable = false)]
   public string Group;
}
风吹短裙飘 2024-08-13 12:01:01

示例 xml 令人困惑,因为在大多数情况下,序列化程序忽略 null,特别是对于属性。例外情况是 Nullable 与元素一起使用时,例如:

[XmlElement("job")]
public int? Job { get; set; }

可能会导致:

<user ...>
    <job xsi:nil="true" />
</user>

这又与您的示例 xml 非常不同。在一般情况下,可以使用多种方法控制序列化:

  • [XmlElement]IsNullable 属性中
  • 添加 [DefaultValue]
  • 添加 public bool ShouldSerialize{propname}() {...} 方法
  • 添加 [XmlIgnore] public bool {propname}Specified {get {...} set {...} } 属性

但是;如果没有可重复示例来运行您的示例,就不可能完全回答。


重新更新问题; this 应该可以实现这一点,但不清楚这些其他属性是什么(如果为 null,则不希望显示)。

[Serializable, XmlRoot("user")]
public partial class User
{
    [XmlAttribute("id")]
    public int Id {get;set;} // snipped more complex property implementation
    [XmlAttribute("name")]
    public string Username  {get;set;} // ditto
}

The example xml is confusing, because in most cases the serializer will omit nulls, especially for attributes. The exception to this is Nullable<T> when used with elements, for example:

[XmlElement("job")]
public int? Job { get; set; }

Might result in:

<user ...>
    <job xsi:nil="true" />
</user>

Which is again very different to your example xml. In the general case, it is possible to control serialization using a number of methods:

  • the IsNullable property of [XmlElement]
  • adding a [DefaultValue]
  • adding a public bool ShouldSerialize{propname}() {...} method
  • adding a [XmlIgnore] public bool {propname}Specified {get {...} set {...}} property

However; without a repeatable example to run your example against, it is impossible to answer fully.


Re the updated question; this should accomplish that, but it isn't clear what these other properties are (that you want to not show if null).

[Serializable, XmlRoot("user")]
public partial class User
{
    [XmlAttribute("id")]
    public int Id {get;set;} // snipped more complex property implementation
    [XmlAttribute("name")]
    public string Username  {get;set;} // ditto
}
避讳 2024-08-13 12:01:01

从模式的角度来看,您实际上是在说元素是可选的,这可以通过使用 DefaultValue 属性来实现(至少根据文档)。请参阅 http://msdn.microsoft.com /en-us/library/system.xml.serialization.xmlattributes.xmldefaultvalue.aspx

From a schema perspective you are effectively saying that the elements are optional, this can be achieved ( at least according to the documentation ) through use of the DefaultValue attribute. See http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmldefaultvalue.aspx

爱冒险 2024-08-13 12:01:01

您确定

 GetColumnValue<string>("Username"); 

返回 null 而不是空白吗?这似乎是最有可能的解释......

Are you sure

 GetColumnValue<string>("Username"); 

is returning null and not blank? This seems like the most likely explanation...

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