我想将变量中的数据保存到 XML 文件中,并在程序重新启动后对其进行编码

I want to save data from variables into a XML file and coad it after restart of the programm

我想将变量保存在 XML 文件中,如果我再次启动程序,我想将保存的数据加载到 programm/into 变量中。我该怎么做? ATM创建了XML,但是变量的数据始终保存为“0,0”。

See Picture

        double fixcosts = 0.0;
        double costs = 0.0;
        Maths math = new Maths();

        int month = DateTime.Today.Month;

        if (month == 1)
        {

            XDocument January_costs_categories = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                                                                new XElement("CostsCategories",
                                                                    new XElement("Fixcosts", fixcosts + " Euro"),
                                                                    new XElement("Costs", costs + " Euro")));


            Console.WriteLine("\n JANUARY COSTS \n");
            Console.WriteLine($"Fixcosts = {fixcosts}");
            Console.WriteLine($"Costs = {costs}");

            Console.WriteLine("\n\nHow much you wanna add?");
            double howMuch = Convert.ToDouble(Console.ReadLine());

            fixcosts = math.Addieren(fixcosts, howMuch);
            Console.Clear();

            Console.WriteLine("\n JANUARY COSTS \n");
            Console.WriteLine($"Fixcosts = {fixcosts}");
            Console.WriteLine($"Costs = {costs}");



            January_costs_categories.Save(@"C:\Users\tobia\Desktop\Neuer Ordner\fixcosts_january.xml");
        }
    }
}

}

您正在为变量赋值之前创建 XDocument。将带有 new XDocument(... 的行移动到调用 Save().

之前