C# SAP Soap error: The value 07:41:39.4780076+03:00 is not a valid time. It does not correspond to the XML format for ABAP

C# SAP Soap error: The value 07:41:39.4780076+03:00 is not a valid time. It does not correspond to the XML format for ABAP

当我尝试使用方法

将当前时间添加到 SAP 中的时间字段时出现错误
DateTime.UtcNow.ToUniversalTime();

然后我收到错误信息:

The value 07:41:39.4780076+03:00 is not a valid time. It does not correspond to the XML format for ABAP.

这适用于日期时间字段,但不适用于时间字段

DateTime.UtcNow;

我已经尝试搜索但是没有很好的例子。

编辑:

这和我的问题一样

这些问题非常接近,但是有没有一种方法可以在不对 WDSL 自动生成的代码进行重大更改的情况下做到这一点

Serializing a DataType="time" field using XmlSerializer

Serializing DateTime to time without milliseconds and gmt

我找到了答案。它基于上面的链接,主要是这个

我需要制作新的部分 class,它实现了生成日期时间和 XML 元素的新方法。旧方法设置为 [XmlIgnore],新方法生成 XML 个元素。

生成Reference.cs

namespace MyService.SAPNamespace {

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://sap.com/....")]
public partial class SAPClassRequestBundle : object, System.ComponentModel.INotifyPropertyChanged{

    private System.DateTime zTimeField;

    [System.Xml.Serialization.XmlIgnore] //This line is added so that its not used
    [System.Xml.Serialization.XmlElementAttribute(Namespace="http://sap.com/....", DataType="time", Order=106)]
    public System.DateTime ZTime {
        get {
            return this.zTimeField;
        }
        set {
            this.zTimeField = value;
            this.RaisePropertyChanged("ZTime");
        }
    }

}

}

我的新 class SapWsdlFix.cs 实现了新方法 ZTimeString

namespace MyService.SAPNamespace {
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://sap.com/....")]
public partial class SAPClassRequestBundle : object, System.ComponentModel.INotifyPropertyChanged {

    [System.Xml.Serialization.XmlElementAttribute(ElementName = "ZTime", Namespace = "http://sap.com/....", DataType = "string", Order = 106)]
    public System.String ZTimeString {
        get
        {
            return this.zTimeField.ToString("HH:mm:ss");
        }
        set
        {
            this.zTimeField = System.DateTime.ParseExact(value, "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
            this.RaisePropertyChanged("ZTime");
        }
    }
}

}

自定义 class 的简称差异。

  • ElementName-属性设置为与原始Reference.cs方法名称相同
  • DataType-属性从时间更改为字符串

    [System.Xml.Serialization.XmlElementAttribute(ElementName = "ZTime", Namespace = "http://sap.com/....", DataType = "string", Order = 106)]

找到了如何在不编辑生成的代码的情况下做到这一点的解决方案。

public partial class SAPClassRequestBundle : object, System.ComponentModel.INotifyPropertyChanged 
{
    [System.Xml.Serialization.XmlElementAttribute(ElementName = "ZTime", Namespace = "http://sap.com/....", DataType = "string", Order = 107)]
public System.String ZTimeString 
    {
        get
        {
            return this.zTimeField.ToString("HH:mm:ss");
        }
        set
        {
            this.zTimeField = System.DateTime.ParseExact(value, "HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
            this.RaisePropertyChanged("ZTime");
        }
    }

    public bool ShouldSerializeZTime()
    {
        return false;
    }
}

所以基本上让它工作只需将 Order 参数分配给未采用的值并添加名称匹配模式 ShouldSerialize{FieldName} 的方法并返回 false。

在此处找到解决方案:

我从 SAP ByD SOAP API 得到了同样的错误。 DateTimeTime有很大区别。

如果数据类型为 Time,则值应遵循此格式 hh:mm:ss,例如 13:15:30.

如果它是 DateTime,那么它应该具有这种格式:yyy-mm-ddThh:mm:ssZ。例如 2021-09-27T12:30:00Z.