如何判断字符串元素是null还是null? [英] How to tell if a string element is null or really null?

查看:96
本文介绍了如何判断字符串元素是null还是null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题所在。


你有一个班级,


班级人员

{

public int id;

public string firstname;

public string lastname;

}
<当你通过webservice公开它时,


[WebMethod]

public void UpdatePerson(Person)

{

...

}


如何知道是否指定了字段值?对于价值

类型,有匹配的XXXXSpecified伙伴字段,但对于字符串类型,

似乎不是一个。

因此,如果传入的Xml看起来像这样:

< Person>

< firstname> Jiho< / firstname>

< /将>


反序列化为Person对象时,lastname字段将包含null,

并不告诉我该字段是否只是缺失(因此安全地忽略了)

或lastname字段应设置为null(例如在数据库中)。


任何想法如何解决这个问题? />
先谢谢。

Jiho Han

高级软件工程师

Infinity信息系统

销售技术专家

电话:212.563.4400 x216

传真:212.760.0540
jh ** @ infinityinfo.com
www.in finityinfo.com

Here''s the issue.

You have a class,

Class Person
{
public int id;
public string firstname;
public string lastname;
}

when you expose it via webservice,

[WebMethod]
public void UpdatePerson(Person)
{
...
}

How do I know whether a field value has been specified or not? For value
types, there is that matching XXXXSpecified buddy field, but for string type,
there doesn''t seem to be one.
So if incoming Xml looks something like this:
<Person>
<firstname>Jiho</firstname>
</Person>

when deserialized into a Person object, lastname field will contain null,
which doesn''t tell me whether the field is simply missing (thus safely ignored)
or lastname field should be set to null (in the database, for example).

Any idea how to go about this?
Thanks in advance.
Jiho Han
Senior Software Engineer
Infinity Info Systems
The Sales Technology Experts
Tel: 212.563.4400 x216
Fax: 212.760.0540
jh**@infinityinfo.com
www.infinityinfo.com

推荐答案

就这么简单,


if(IsNullOrEmpty) (person.lastname))

// Echo是null

else

//回声不是空的


仅对字符串类型有效。对于其他类型的引用类型,你需要检查它是否等于null。


if(person.lastname == null)

// Echo Is null

else

// Echo不为空


Jiho Han < ji ***** @ yahoo.com在留言中写道

新闻:a1 ************************* *@msnews.microsoft .com ...
It is as simple as this,

if( IsNullOrEmpty(person.lastname) )
// Echo Is null
else
// Echo Is not null

That is only valid for string types. For other kind of reference type, you
should check if it is equal to null.

if( person.lastname == null)
// Echo Is null
else
// Echo Is not null

"Jiho Han" <ji*****@yahoo.comwrote in message
news:a1**************************@msnews.microsoft .com...

这就是问题。


你有一个班级,


班级人员

{

public int id;

public string firstname;

公共字符串姓氏;

}


当你通过网络服务公开它时,


[ WebMethod]

public void UpdatePerson(Person)

{

...

}


如何知道是否指定了字段值?对于价值

类型,有匹配的XXXXSpecified伙伴字段,但对于字符串

类型,似乎没有。

因此,如果传入的Xml看起来像这样:

< Person>

< firstname> Jiho< / firstname>

< /将>


反序列化为Person对象时,lastname字段将包含null,

并不告诉我该字段是否只是缺失(因此安全

忽略)或lastname字段应设置为null(在数据库中,对于

示例)。


任何想法怎么样呢?

提前致谢。


Jiho Han

高级软件工程师

无限信息系统

销售技术专家

电话:212.563.4400 x216

传真:212.760.0540
jh ** @ infinityinfo.com
www.infinityinfo.com

Here''s the issue.

You have a class,

Class Person
{
public int id;
public string firstname;
public string lastname;
}

when you expose it via webservice,

[WebMethod]
public void UpdatePerson(Person)
{
...
}

How do I know whether a field value has been specified or not? For value
types, there is that matching XXXXSpecified buddy field, but for string
type, there doesn''t seem to be one.
So if incoming Xml looks something like this:
<Person>
<firstname>Jiho</firstname>
</Person>

when deserialized into a Person object, lastname field will contain null,
which doesn''t tell me whether the field is simply missing (thus safely
ignored) or lastname field should be set to null (in the database, for
example).

Any idea how to go about this?
Thanks in advance.
Jiho Han
Senior Software Engineer
Infinity Info Systems
The Sales Technology Experts
Tel: 212.563.4400 x216
Fax: 212.760.0540
jh**@infinityinfo.com
www.infinityinfo.com



Pablo,


也许,它更像是一个设计问题...


如果你想将一个字段设置为null(想想数据库),你会如何指定<来自客户端的


如果你不包括该字段,它在服务器上是空的。如果你将它设置为

null,它在服务器上仍然是空的。

我的观点是,似乎无法知道客户是否打算

a字段设置为null与跳过字段进行处理因为它是从xml中缺少


ie)

< Person>

< Lastname> Han< / Lastname>

< Firstname />

< ; /人>





< Person>

< Lastname> Han< / Lastname>

< / Person>


反序列化为相同的对象状态。
Pablo,

Perhaps, it is more of a design question...

If you want a field to be set to null (think database), how would you specify
that from the client side?
If you don''t include the field, it''s null on the server. If you set it to
null, it''s still null on the server.
My point is that there seems to be no way of knowing whether the client intended
a field to be set to null vs. skip the field for processing becasue it''s
missing from the xml.
i.e.)
<Person>
<Lastname>Han</Lastname>
<Firstname/>
</Person>

and

<Person>
<Lastname>Han</Lastname>
</Person>

deserializes into an identical object state.

它是就这么简单,


if(IsNullOrEmpty(person.lastname))

// Echo Is null

else

// Echo不为空

仅对字符串类型有效。对于其他类型的引用类型,

你应该检查它是否等于null。


if(person.lastname == null)

// Echo Is null

else

// Echo不为空

Jiho Han < ji ***** @ yahoo.com在留言中写道

新闻:a1 ************************* *@msnews.microsoft .com ...
It is as simple as this,

if( IsNullOrEmpty(person.lastname) )
// Echo Is null
else
// Echo Is not null
That is only valid for string types. For other kind of reference type,
you should check if it is equal to null.

if( person.lastname == null)
// Echo Is null
else
// Echo Is not null
"Jiho Han" <ji*****@yahoo.comwrote in message
news:a1**************************@msnews.microsoft .com...

>这是问题所在。

你有一个班级,

班级人员
{public int id;
公共字符串名字;
公共字符串姓氏;
}
当你公开它时通过webservice,

[WebMethod]
public void UpdatePerson(Person)
{
...
}
我怎么知道是否是否指定了字段值?对于

类型,有匹配的XXXXSpecified伙伴字段,但对于
字符串
类型,似乎没有。
所以如果传入Xml看起来像这样:
< Person>
< firstname> Jiho< / firstname>
< / Person>
当反序列化为Person对象时,lastname字段将包含
null,它不会告诉我字段是否只是缺失(因此安全地被忽略)或者lastname字段应该设置为null(例如在
数据库中)。

知道如何解决这个问题吗?
提前致谢。
Jiho Han
高级软件工程师
Infinity Info Systems
销售技术专家
电话:212.563.4400 x216
传真:212.760.0540
jh ** @ infinityinfo.com
www.infinityinfo.com



XML文档t,特别是一个SOAP文档,并不像你想象的那么简单。
。它可以指示值是否为空。

示例:


< SomeObject xsi:nil =" true" />


-

HTH,


Kevin Spencer

Microsoft MVP

专业鸡肉沙拉炼金术士


顺序,选择,迭代。


" Jiho Han" < ji ***** @ yahoo.com在留言中写道

新闻:a1 ************************* *@msnews.microsoft .com ...
An XML document, and in particular, a SOAP document, is not quite as simple
as you seem to think. It can indicate whether the value is null or not.
Example:

<SomeObject xsi:nil="true" />

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Sequence, Selection, Iteration.

"Jiho Han" <ji*****@yahoo.comwrote in message
news:a1**************************@msnews.microsoft .com...

Pablo,


也许,这更像是一个设计问题。 ..


如果你想将一个字段设置为null(想想数据库),你如何从客户端指定


如果你不包括该字段,它在服务器上是空的。如果你将它设置为

null,它在服务器上仍然是空的。

我的观点是,似乎无法知道客户端是否为

意图将字段设置为null而不是跳过字段进行处理

因为xml中缺少它。

ie)

< Person>

< Lastname> Han< / Lastname>

< Firstname />

< / Person>




< Person>

< Lastname> Han< / Lastname>

< / Person>


反序列化为相同的对象状态。
Pablo,

Perhaps, it is more of a design question...

If you want a field to be set to null (think database), how would you
specify that from the client side?
If you don''t include the field, it''s null on the server. If you set it to
null, it''s still null on the server.
My point is that there seems to be no way of knowing whether the client
intended a field to be set to null vs. skip the field for processing
becasue it''s missing from the xml.
i.e.)
<Person>
<Lastname>Han</Lastname>
<Firstname/>
</Person>

and
<Person>
<Lastname>Han</Lastname>
</Person>

deserializes into an identical object state.

>它是这很简单,

if(IsNullOrEmpty(person.lastname))
// Echo是null

// Echo不是null
那个仅对字符串类型有效。对于其他类型的引用类型,
你应该检查它是否等于null。

if(person.lastname == null)
// Echo是null
其他
// Echo不是空的
Jiho Han < ji ***** @ yahoo.com写了留言
新闻:a1 ************************** @ msnews。 microsof t.com ...
>It is as simple as this,

if( IsNullOrEmpty(person.lastname) )
// Echo Is null
else
// Echo Is not null
That is only valid for string types. For other kind of reference type,
you should check if it is equal to null.

if( person.lastname == null)
// Echo Is null
else
// Echo Is not null
"Jiho Han" <ji*****@yahoo.comwrote in message
news:a1**************************@msnews.microsof t.com...

>>这是问题所在。

你有一个班级,
班级人员
{public int id;
公共字符串名字;
公共字符串姓氏;
}
当你通过webservice,

[WebMethod]
public void UpdatePerson(Person)
{
...
}
我怎么知道是否一个字段值是否已指定?对于

类型,有匹配的XXXXSpecified伙伴字段,但对于
字符串
类型,似乎没有。
所以如果传入Xml看起来像这样:
< Person>
< firstname> Jiho< / firstname>
< / Person>
当反序列化为Person对象时,lastname字段将包含
null,它不会告诉我字段是否只是缺失(因此安全地被忽略)或者lastname字段应该设置为null(例如在
数据库中)。

知道如何解决这个问题吗?
提前致谢。
Jiho Han
高级软件工程师
Infinity Info Systems
销售技术专家
电话:212.563.4400 x216
传真:212.760.0540
jh ** @ infinityinfo.com
www.infinityinfo.com




这篇关于如何判断字符串元素是null还是null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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