简体   繁体   中英

C# xml Serialize with XmlIgnore and Deserialize problem

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM