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?

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? Commented Feb 25, 2019 at 4:54
  • Only way is to have a duplicate model without XmlIgnore attribute, then it will work. Commented Feb 25, 2019 at 4: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 Commented Feb 25, 2019 at 5: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. Commented Feb 25, 2019 at 6:26

1 Answer 1

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);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Then [XmlIgnore] doesn't effect to deserialize?
it does. which is why your "FromDate" fields do not get serialized. which is also why you will lose the uncalculated value. learn.microsoft.com/en-us/dotnet/api/…
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.