asmx 服务可以不返回任何内容而不返回 null 吗?
我有一个正在维护的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
XmlElementAttribute.IsNullable 属性
如果 IsNullable 属性为 false,则不会为已设置为 null 引用(在 Visual Basic 中为 Nothing)的类成员生成任何 XML 元素。
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).
示例 xml 令人困惑,因为在大多数情况下,序列化程序将忽略 null,特别是对于属性。例外情况是
Nullable
与元素一起使用时,例如:可能会导致:
这又与您的示例 xml 非常不同。在一般情况下,可以使用多种方法控制序列化:
[XmlElement]
的IsNullable
属性中[DefaultValue]
public bool ShouldSerialize{propname}() {...}
方法[XmlIgnore] public bool {propname}Specified {get {...} set {...} }
属性但是;如果没有可重复示例来运行您的示例,就不可能完全回答。
重新更新问题; this 应该可以实现这一点,但不清楚这些其他属性是什么(如果为 null,则不希望显示)。
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:Might result in:
Which is again very different to your example xml. In the general case, it is possible to control serialization using a number of methods:
IsNullable
property of[XmlElement]
[DefaultValue]
public bool ShouldSerialize{propname}() {...}
method[XmlIgnore] public bool {propname}Specified {get {...} set {...}}
propertyHowever; 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).
从模式的角度来看,您实际上是在说元素是可选的,这可以通过使用 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
您确定
返回 null 而不是空白吗?这似乎是最有可能的解释......
Are you sure
is returning null and not blank? This seems like the most likely explanation...