1

我在我的 C# webservice 项目中遇到了一个可为空的属性问题。

public class Header
{
    [DataMember]
    public long Id { get; set; }
    [DataMember]
    public long SportId { get; set; }
    [DataMember]
    public long TournamentId { get; set; }
    [DataMember]
    public string TournamentName { get; set; }
    [DataMember]
    public string TournamentShortName { get; set; }
    [DataMember]
    public long StatusId { get; set; }
    [DataMember]
    public string StatusName { get; set; }
    [DataMember]
    public string StatusShortName { get; set; }
    [DataMember]
    public DateTime MatchDate { get; set; }
    [DataMember]
    public string MatchTime { get; set; }
    [DataMember]
    public DateTime? PeriodStartDate { get; set; }   

    [DataMember]
    public int? Quarter1HomeTeamScore { get; set; }
    [DataMember]
    public int? Quarter1AwayTeamScore { get; set; }
    [DataMember]
    public int Quarter2HomeTeamScore { get; set; }
    [DataMember]
    public int Quarter2AwayTeamScore { get; set; }
    [DataMember]
    public int Quarter3HomeTeamScore { get; set; }
    [DataMember]
    public int Quarter3AwayTeamScore { get; set; }
    [DataMember]
    public int Quarter4HomeTeamScore { get; set; }
    [DataMember]
    public int Quarter4AwayTeamScore { get; set; }

}

这是序列化后的soap xml:

<LiveMatchList>
<Items>
    <ListMatch>
        <Header>
        <Quarter1HomeTeamScore xsi:nil="true"/>
        <Quarter1AwayTeamScore xsi:nil="true"/>
        <Quarter2HomeTeamScore xsi:nil="true"/>
        <Quarter2AwayTeamScore xsi:nil="true"/>
        <Quarter3HomeTeamScore xsi:nil="true"/>
        <Quarter3AwayTeamScore xsi:nil="true"/>
        <Quarter4HomeTeamScore xsi:nil="true"/>
        <Quarter4AwayTeamScore xsi:nil="true"/>
        <Id>138906</Id>
        <SportId>2</SportId>
        <TournamentId>8</TournamentId>
        <TournamentName> Eurocup</TournamentName>
        <TournamentShortName> AVK</TournamentShortName>
        <StatusId>12</StatusId>
        <StatusName> 1. Periyot</StatusName>
        <StatusShortName> 1.P</StatusShortName>
        <MatchDate>2013-03-20T00:00:00</MatchDate>
        <MatchTime> 21:30</MatchTime>
        <PeriodStartDate xsi:nil="true"/>           
    </Header>
</ListMatch>

我试图在http://www.xmlgrid.net/xsi:nil = "true"中查看 xml,但由于节点而无法查看。在另一个使用此 Web 服务的项目中,我在反序列化为对象时遇到了错误的 xml 格式错误。

如何忽略 null 属性?或者有没有其他方法可以解决这个问题?

我试过了 :

[DataMember(EmitDefaultValue = false)]
public int? Quarter1HomeTeamScore { get; set; }

[DataMember(EmitDefaultValue = false)]
public int Quarter1HomeTeamScore = 0
4

1 回答 1

1

像这样添加EmitDefaultValue属性:

[DataMember(EmitDefaultValue = false)]

更新

尝试IsRequired属性,例如

 [DataMember(IsRequired = false)]

正如您评论的那样,它对您不起作用,请查看

如何从 wcf 中的响应中删除 null DataMember 属性

于 2013-03-21T09:38:45.010 回答