Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#写入XML文件不会保存对象_C#_Xml_File_Object_Save - Fatal编程技术网

C#写入XML文件不会保存对象

C#写入XML文件不会保存对象,c#,xml,file,object,save,C#,Xml,File,Object,Save,我有一些Java方面的经验。今天我开始用C语言编程,因为我喜欢Visual Studio 作为练习,我正在构建一个管理公司所有员工的系统。 我正在处理一个名为函数的类: public class Function { private String functionname; private String department; private double hourpay; public String getFunc() { retu

我有一些Java方面的经验。今天我开始用C语言编程,因为我喜欢Visual Studio

作为练习,我正在构建一个管理公司所有员工的系统。 我正在处理一个名为
函数
的类:

public class Function
{

    private String functionname;
    private String department;
    private double hourpay;


    public String getFunc()
    {
        return functionname;
    }

    public String getDepartement()
    {
        return department;

    }

    public double getPay()
    {
        return hourpay;
    }

    public String toString()
    {
        return ("Function: " + functionname + "\n" + "Department: " + department + "\n" + "Hourly Pay: " + hourpay);
    }

    public void setFunctionName(string functionname)
    {
        this.functionname = functionname;

    }

    public void setDepartment(String department)
    {
        this.department = department;

    }

    public void setPay(double pay)
    {
        this.hourpay = pay;
    }
一个非常简单的函数建模基本类,现在我想将函数保存在一个XML文件中

我的想法是: 创建一个函数后,我将其放入一个名为“函数”的列表中(列表函数) 我将列表写入一个XML文件

如果要更新XML文件,只需加载列表,添加一个新函数,然后将其覆盖到XML文件中

我让我的类函数是这样的

public class Functions{

    public List<Function> functions = new List<Function>();

    public void addFunction(Function func){
        functions.Add(func);

    }

    public void writeFunctions()
    {
            System.Xml.Serialization.XmlSerializer writer =
                new System.Xml.Serialization.XmlSerializer(typeof(Functions));
            System.IO.StreamWriter file = new System.IO.StreamWriter(
             @"C:\CDatabase\Functions.xml");
            writer.Serialize(file, this);
            file.Close();
    }

}
它将创建XML文件(如果不存在),该文件位于XML文件中:

<Functions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <functions>
    <Function />
  </functions>
</Functions>

它是空的,我不明白为什么以及如何修复它

我想我想得太容易了,但我找不到解决办法

提前感谢,

Chris不使用像Java这样的getter和setter方法。按如下方式创建您的属性:

private string functionname;

public string functionname
{
  get { return functionname; }
  set { functionname = value; }
}

建议使用属性而不是getter/setter函数——它们是C#的一个特性,C#经常使用

例如,而不是:

public double getPay()
{
    return hourpay;
}
尝试:

或者,如果希望能够在getter/setter中执行操作:

private double hourPay;
public double HourPay
{
    get
    {
        return hourPay;
    }
    set
    {
        hourPay = value;
    }
}

通过以这种方式使用公共属性,XML序列化程序应该生成您期望的文件。

什么是schoonmaker?
c!=java
,在发布此问题之前,您是否尝试过查找一些关于如何在c#中完成xml序列化的示例?提示:搜索
c#xml序列化
:)您的函数类需要属性。公共成员(属性和字段)将被序列化。在xml序列化中,它们将成为标记。通过实现
ISerializable
可以显式地对类进行序列化,并显式地处理StreamingContext和SerializationInfo对象。请参阅特殊构造函数以进行反序列化:
public double HourPay { get; set; }
private double hourPay;
public double HourPay
{
    get
    {
        return hourPay;
    }
    set
    {
        hourPay = value;
    }
}