【问题标题】:WSDL time format is ignored from Visual StudioWSDL 时间格式被 Visual Studio 忽略
【发布时间】:2015-04-01 15:02:12
【问题描述】:

来自客户的 WSDL 文件使用以下语法指定时间数据类型:<xsd:simpleType name="time"><xsd:restriction base="xsd:time"><xsd:pattern value="[0-9]{2}:[0-9]{2}:[0-9]{2}"/></xsd:restriction></xsd:simpleType>

我将 WSDL 文件作为“Web 参考”(不是服务参考)包含在 Visual Studio C# 项目中。生成此代码:

[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="time")]
public System.DateTime I_TIMETO {
get {
     return this.i_TIMETOField;
}
set {
     this.i_TIMETOField = value;
}

}

问题在于,在生成的负载中,WSDL 文件中的模式 ([0-9]{2}:[0-9]{2}:[0-9]{2}) 被完全忽略. IE。有效载荷如下所示:

<I_TIMETO xmlns="">17:11:00.0000000+01:00</I_TIMETO> 

代替:

<I_TIMETO xmlns="">17:11:00</I_TIMETO> 

无法更改 Web 服务,我不想更改自动生成的代码。

【问题讨论】:

  • 我不是专家,但我使用的是 WCF 服务,并且认为无法将格式存储在 DateTime 中。我所做的只是传递 DateTime 变量,服务会自行计算出来。另一方面,如果我要传递 DateTime 的字符串值,我需要在客户端和服务器之间有一个标准格式字符串。
  • 遗憾的是,它是一个 SAP 网络服务,所以我无法更改界面。

标签: c# visual-studio-2013 wsdl time-format


【解决方案1】:

我认为没有好的解决方案,所以你必须编辑自动生成的代码。

创建自动生成代码的部分类,并在其中添加格式正确的字符串属性:

[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, 
 DataType = "string", ElementName = "I_TIMETO")]
public string I_TIMETO_STR
{
    get
    {
        return this.i_TIMETOField.ToString("HH:mm:ss");
    }
    set
    {
        this.i_TIMETOField = DateTime.ParseExact(value, "HH:mm:ss", CultureInfo.InvariantCulture);
    }
}

现在转到自动生成的属性并添加一个 XmlIgnore:

[System.Xml.Serialization.XmlIgnore] 
public System.DateTime I_TIMETO{...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多