C#SAP Soap错误:值07:41:39.4780076+03:00不是有效时间.它与ABAP的XML格式不对应



当我试图使用方法将当前时间添加到SAP中的时间字段时出错

DateTime.UtcNow.ToUniversalTime();

然后我得到错误消息:

07:41:39.4780076+03:00不是有效时间。它没有对应于ABAP的XML格式。

这适用于Datetime字段,但不适用于Time字段

DateTime.UtcNow;

我试过搜索,但没有好的例子。

编辑:

这与我的问题相同

Visual Studio 忽略WSDL时间格式

这些问题很接近,但有没有一种方法可以做到这一点,而不会对WDSL 自动生成的代码进行大的更改

串行化DataType=";时间";使用XmlSerializer 的字段

序列化DateTime到时间(不含毫秒和gmt )

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

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 param分配给未取值,并添加具有名称匹配模式ShouldSerialize{FieldName}的方法,然后返回false。

在此处找到解决方案:https://stackoverflow.com/a/8090247/1104587

我找到了答案。它是基于上面的链接,主要是这个https://stackoverflow.com/a/2402568/4845680

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

生成的引用.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");
        }
    }
}

}

我的新类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");
        }
    }
}

}

差异是自定义类的缩写。

  • ElementName属性设置为与原始Reference.cs方法名称相同
  • DataType属性从Time 更改为String

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

我从SAP ByD SOAP API中得到了相同的错误。CCD_ 2和CCD_。

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

如果它是DateTime,那么它应该具有以下格式:yyy-mm-ddThh:mm:ssZ。例如CCD_ 8。

相关内容

最新更新