问题
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
回答1:
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
回答2:
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)]
来源:https://stackoverflow.com/questions/36327950/c-sharp-sap-soap-error-the-value-074139-47800760300-is-not-a-valid-time-it