1
LxSerializer .Serialize() throws an exception when converting LxDateTime to xs:date
Question asked by P M - 9/17/2020 at 2:11 PM
Answered
I'm getting an exception when trying to serialize object using LxSerializer:  
"'DataWystawienia  could not be converted to a string 'The LxDateTime(DateTime) could not be converted to a xs:date".
This is how I initialise it: ...DataWystawienia  = LxDateTime.Today;
This is the property definition:
[LxElementValue("DataWystawienia", "(http...)crd.gov.pl/wzor/2020/05/08/9393/", LxValueType.Value, XsdType.XsdDate, MinOccurs = 1, MaxOccurs = 1, Pattern = "((\\d{4})-(\\d{2})-(\\d{2}))", MaxInclusive = "2030-01-01", MinInclusive = "2006-01-01")]
public LiquidTechnologies.XmlObjects.LxDateTime DataWystawienia { get; set; }
Is there something I'm doing wrong? Or is it a bug?
 

5 Replies

Reply to Thread
0
Liquid Support Replied
Employee Post
I've mocked this up and I can't repeat the problem. 
Your code looks OK
xxx.DataWystawienia  = LxDateTime.Today;

The code that does the conversion looks like this. So it looks like the value you are setting into DataWystawienia is a LxDateTime of type DateTime (Today returns an LxDateTime of type Date).

            if (val.Type != LxDateTimeType.Date)             
            {
                 stringValue = "";
                 errorType = ConversionErrorType.FormatError;
                 errorMsg = $"The LxDateTime({val.Type}) could not be converted to a xs:date";
                 return false;
             }
I've changed the code to make it automatically convert to the target LxDateTimeType if the conversion is possible.

          if (val.TryChangeType(LxDateTimeType.Date, out LxDateTime date) == false)
            {
                stringValue = "";
                errorType = ConversionErrorType.FormatError;
                errorMsg = $"The LxDateTime({val.Type}) could not be converted to a xs:date";
                return false;
            }

But I think the problem is you are passing a LxDateTime of type DateTime into a property that expects a LxDateTime of type Date.
0
P M Replied
Thanks for the reply. You were absolutely right, it was another property down the line. Having said that, I'm not sure what is the correct way of creating LxDateTime of type Date when passing System.DateTime as parameter to the constructor. I tried using System.DateTime.Date but this is still treated not as Date type.
I guess I could use the constructor where you specify the long list of parameters LxDateTime(type, year, month, day, hour, minute....), but this is far from ideal...

1
Liquid Support Replied
Employee Post
The interim work around is as you suggest, is either using LxDateTime.CreateDate or the long hand constructor. The next point release (18.0.13) will contain the changes that should make this unnessarsay, as it will be able to imlicitly convert the DateTime to a Date if required. Its currently running regression tests, if all goes well it will be out early next week.
0
P M Replied
Thank you for the reply.

As a suggestion you could consider constructor 
parameters defaulting to 0 or just have overloaded constructor.
0
Liquid Support Replied
Employee Post Marked As Answer
This issue has been fixed in v18.0.13:

Reply to Thread