简体   繁体   English

C#xml使用XmlIgnore进行序列化和反序列化问题

[英]C# xml Serialize with XmlIgnore and Deserialize problem

Hello I have problem with deserialize xml 您好,我对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 现在,我获得仅包含FromDateTimeCal和ToDateTimeCal的XML文件

but the problem begin when I deserialize them. 但是问题是当我反序列化它们时开始的。

I using deserialize with ReportViewModel class 我对ReportViewModel类使用反序列化

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. 我猜问题是没有设置FromDateTime和ToDateTime属性。

Can I serialize and deserialize with the same class? 我可以使用同一类进行序列化和反序列化吗?

your FromDateTime and ToDateTime are never being assigned a value when you Deserialize.. 反序列化时,永远不会为您的FromDateTimeToDateTime分配值。

your set inside your cal properties are not doing anything with the value passed to them. 您在cal属性中设置的值对传递给它们的值没有任何作用。

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. 当获得ToDateTimeCalFromDateTimeCal的值时,将对已经计算出的值再次运行相同的计算。 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. 由于您在计算中使用的是当前日期,并且您从未将该日期保存在文件中,因此您无法通过反转值来确定FromDateTime是什么。 Unless you read the date from the file itself. 除非您从文件本身读取日期。 It would make more sense to serialze the FromDateTime instead. 相反,序列化FromDateTime会更有意义。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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