1

我有一个来自 Web 服务服务器的 WSDL,它的类型包括以下内容:

<xs:element minOccurs="0" name="countryID" type="xs:int"/>

这应该表示一个名为 countryID 的可选 int 元素。当我针对它运行 svcutil.exe(来自 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin)时,我得到以下生成的代码:

[System.Runtime.Serialization.DataMemberAttribute()]
public int countryID
{
    get
    {
        return this.countryIDField;
    }
    set
    {
        this.countryIDField = value;
    }
}

未在 DataMemberAttribute 上设置“IsRequired=true”属性。但是我看不到一种方法来表明我希望给定的实例不存在!一些谷歌搜索显示我可以修改生成的代码(我讨厌这样做)以使用“DataMemberAttribute(EmitDefaultValue=false)”(参见http://msdn.microsoft.com/en-us/library/aa347792.aspx?ppud= 4)。但是如果我这样做,我不能使用国家 ID 值为零,因为这将被解释为默认值,并且不会生成国家 ID 元素!

如果我添加“/serializer:XmlSerializer”命令行选项,将生成以下内容:

[System.Xml.Serialization.XmlElementAttribute(Order=0)]
public int countryID
{
    get
    {
        return this.countryIDField;
    }
    set
    {
        this.countryIDField = value;
    }
}

///
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool countryIDSpecified
{
    get
    {
        return this.countryIDFieldSpecified;
    }
    set
    {
        this.countryIDFieldSpecified = value;
    }
}

这似乎应该起作用:我只是将 countryIDFieldSpecified 设置为“false”。

这应该如何与 DataContractSerializer 一起使用?

谢谢!

4

0 回答 0