0

Hello I have problem with deserialize xml

First I have class like that

public class ReportsViewModel
{
    private DateTime fromDateTime;
    [XmlIgnore]
    public DateTime FromDateTime
    {
        get { return fromDateTime; }
        set
        {
            fromDateTime = value;

        }
    }
    [XmlElement]
    public int FromDateTimeCal
    {
        get
        {
            return fromDateTime.Subtract(DateTime.Today).Days;
        }
        set
        {
            var a = fromDateTime.Subtract(DateTime.Today).Days;
            a = value;
        }
    }

    private DateTime toDateTime;
    [XmlIgnore]
    public DateTime ToDateTime
    {
        get { return toDateTime; }
        set
        {
            toDateTime = value;

        }
    }
    [XmlElement]
    public int ToDateTimeCal
    {
        get
        {
            return ToDateTime.Subtract(DateTime.Today).Days;
        }
        set
        {
            var a = ToDateTime.Subtract(DateTime.Today).Days;
            a = value;
        }
    }
}

And then I serialize them

ReportsViewModel reportVM = new ReportsViewModel();
    reportVM.FromDateTime = new DateTime(2019, 02, 18);
    reportVM.ToDateTime = new DateTime(2019, 02, 22);
    using (StreamWriter sw = new StreamWriter(@"D:\Temp\Report.xml"))
    {           
        XmlSerializer xml = new XmlSerializer(typeof(ReportsViewModel));
        xml.Serialize(sw, reportVM);            
    }

Now I get XML file that contain only FromDateTimeCal and ToDateTimeCal

but the problem begin when I deserialize them.

I using deserialize with ReportViewModel class

using (StreamReader sw = new StreamReader(@"D:\Temp\Report.xml"))
            {
                XmlSerializer xml = new XmlSerializer(typeof(ReportsViewModel));
                ReportsViewModel reportVM = (ReportsViewModel)xml.Deserialize(sw);
                reportVM.Dump();
                reportVM.FromDateTimeCal.Dump();
                reportVM.ToDateTimeCal.Dump();
            }   

It didn't work. I guess the problem is FromDateTime and ToDateTime property wasn't set.

Can I serialize and deserialize with the same class?

Oscar.Y
  • 15
  • 4
  • Can you please explain what you expect setter for `FromDateTimeCal` to do? I looked at it but can't understand what you wanted to achieve (as it really just fancy way to do absolutely noting)... Clearly that code does not set `fromDateTime`... did you wanted it to? – Alexei Levenkov Feb 25 '19 at 04:54
  • Only way is to have a duplicate model without XmlIgnore attribute, then it will work. – Kishore Kolla Feb 25 '19 at 04:55
  • Actually `FromDateCal` and `ToDateCal` is used for calculation result of `FromDateTime` and ToDate and I want only `FromDateCal` and `ToDateCal` are in xml file – Oscar.Y Feb 25 '19 at 05:32
  • Here is how de-serialization happens, let's go node by node. It finds `FromDateTimeCal`, applies that to setter of your property, in which variable `a` is assigned and its lost when execution leaves the scope, same happens for `ToDateTimeCal`, also `FromDateTime` and `ToDateTime` will never be set. This code will always do serialization only, deserialization will not be possible, also even if you changed both `cal` setters to compute and set value to `FromDateTime` or `ToDateTime`, it always has `DateTime.Today` which keeps changing and the data might be valid only for a month at max. – Ram Kumaran Feb 25 '19 at 06:26

1 Answers1

0

your FromDateTime and ToDateTime are never being assigned a value when you Deserialize..

your set inside your cal properties are not doing anything with the value passed to them.

var a = fromDateTime.Subtract(DateTime.Today).Days;
            a = value;

that line is keeping the value and calculated value inside that block but never forwards the calculated value.

im going to just guess and say what you want is something like:

var a = value.Subtract(DateTime.Today).Days;
            ToDateTime = a;

but then you are going to run into an issue. when you get the value of ToDateTimeCal and FromDateTimeCal, you are going to run the same calculation again, on a already calculated value. Since you are using the current date in the calculation, and you never save that date in the file - you dont have a way to reverse the value to figure out what the FromDateTime was. Unless you read the date from the file itself. It would make more sense to serialze the FromDateTime instead. But if the original date used in the calculation is not necessary, maybe you can do something like:

 [XmlIgnore]
public DateTime FromDateTime
{
    get { return fromDateTime; }
    set
    {
        fromDateTime = value;

    }
}
[XmlElement]
public int FromDateTimeCal
{
    get
    {
        return fromDateTime.Subtract(DateTime.Today).Days;
    }
    set
    {
        fromDateTime = DateTime.Today.AddDays(value);
    }
}
Heriberto Lugo
  • 589
  • 7
  • 19
  • Then `[XmlIgnore]` doesn't effect to deserialize? – Oscar.Y Feb 25 '19 at 08:45
  • it does. which is why your "FromDate" fields do not get serialized. which is also why you will lose the uncalculated value. https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlattributes.xmlignore?view=netframework-4.7.2 – Heriberto Lugo Feb 25 '19 at 16:52
  • my point was that you are doing a calculation on a value and saving that to xml. when you deserialize, your calculation does NOT have a value to use to re-calculate, because you XmlIgnore'd it. so really, you should be saving the uncalculated value, or saving the value that you used in the calculation. and to top it off, when you deserialized, your "set" was not doing anything. it was calculating something, then throwing the value away. it never assigned it to anything. – Heriberto Lugo Feb 25 '19 at 16:55