0

There is an error when I am trying to add current time to Time field in SAP using method

DateTime.UtcNow.ToUniversalTime();

Then I get the error message:

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

This works with Datetime fields but not in Time fields

DateTime.UtcNow;

I have tried to search but there are no good examples.

EDIT:

This is same as my problem

WSDL time format is ignored from Visual Studio

These questions are quite close but is there a way doing this without big changes in automatically generated code from WDSL

Serializing a DataType="time" field using XmlSerializer

Serializing DateTime to time without milliseconds and gmt

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
user4845680
  • 128
  • 2
  • 12

3 Answers3

2

Found the solution how to do that without editing generated code.

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;
    }
}

So basically to get it working just assign Order param to non taken value and add method with name matching pattern ShouldSerialize{FieldName} and returning false.

Solution was found here: https://stackoverflow.com/a/8090247/1104587

Community
  • 1
  • 1
liri2006
  • 591
  • 7
  • 18
0

I have found the answer. It is based on links above an mainly this https://stackoverflow.com/a/2402568/4845680

I needed to make new partial class which implements new method which generates the datetime and the XML element. The old method is set to [XmlIgnore] and the new method generates XML element.

Generated 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");
        }
    }

}

}

My new class SapWsdlFix.cs which implements new method 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");
        }
    }
}

}

Differences in short for the custom class.

  • ElementName-attribute is set to same as the original Reference.cs method name
  • DataType-attribute is changed to String from Time

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

Community
  • 1
  • 1
user4845680
  • 128
  • 2
  • 12
0

I got the same error from a SAP ByD SOAP API. There is a big difference between DateTime and Time.

If the datatype is Time then the value should follow this format hh:mm:ss, for example 13:15:30.

If it's DateTime then it should have this format: yyy-mm-ddThh:mm:ssZ. For example 2021-09-27T12:30:00Z.

Robert Reiz
  • 4,243
  • 2
  • 30
  • 43