XML序列化类结果为空XML

时间:2016-06-14 09:35:11

标签: c# xml unity3d

我正在尝试以可读格式(XML)保存一个类。 问题是,生成的文件只输出为:

<?xml version="1.0" encoding="Windows-1252"?>
<ExtremeLearningMachine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /><?xml version="1.0" encoding="Windows-1252"?>
<ExtremeLearningMachine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /><?xml version="1.0" encoding="Windows-1252"?>
<ExtremeLearningMachine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /><?xml version="1.0" encoding="Windows-1252"?>
<ExtremeLearningMachine xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

这是我的班级:

public class ExtremeLearningMachine {
    public ExtremeLearningMachine()
    {

    }
    int input, hidden; //only have 1 output neuron
    double[,] W1, W2;
    public ExtremeLearningMachine(int inputNeuron, int hiddenNeuron) { input = inputNeuron; hidden = hiddenNeuron; }
    public void train(int dataCount, double[,] trainingSet) {
        //set matrix
        double[,] trainInput = new double[input, dataCount], desireOutput = new double[1, dataCount];
        for (int i = 0; i < dataCount; i++) {
            for (int j = 0; j < input; j++) trainInput[j, i] = trainingSet[i, j];
            desireOutput[0, i] = trainingSet[i, input];
        }
        //W1
        W1 = new double[hidden, input];
        for (int i = 0; i < hidden; i++) { for (int j = 0; j < input; j++)W1[i, j] = Random.value; }
        //hidden
        //double[,] H = new double[hidden, dataCount];
        double[,] H = Matrix.Multiply(W1, trainInput);
        //activation function(binary sigmoid)
        for (int i = 0; i < hidden; i++) {
            for (int j = 0; j < dataCount; j++) H[i, j] = 1f / (1f + Mathf.Exp((float)-H[i, j]));
        }
        //W2
        W2 = Matrix.Multiply(desireOutput, H.PseudoInverse());
    }
    public double test(double[,] set) {//only [~,1] allowed
        double[,] H = Matrix.Multiply(W1, set.Transpose());
        //activation function(binary sigmoid)
        for (int i = 0; i < hidden; i++) H[i, 0] = 1f / (1f + Mathf.Exp((float)-H[i, 0]));
        H = Matrix.Multiply(W2, H);
        return H[0, 0];
    }
}

这是我的保存代码:

void save()
{
    System.Xml.Serialization.XmlSerializer writer =
        new System.Xml.Serialization.XmlSerializer(typeof(ExtremeLearningMachine));

    string path = Directory.GetCurrentDirectory() + "\\ElmTrain.xml";
    System.IO.FileStream file = System.IO.File.Create(path);
    for(int i=0;i<elm.Length;i++)
    writer.Serialize(file, elm[i]);
    file.Close();
}

也是我的加载代码,如果有什么不对(我还没有测试它,因为我无法保存):

void load()
{
    System.Xml.Serialization.XmlSerializer reader =
    new System.Xml.Serialization.XmlSerializer(typeof(ExtremeLearningMachine));
    System.IO.StreamReader file = new System.IO.StreamReader("//ElmTrain.xml");
    elm = (ExtremeLearningMachine[])reader.Deserialize(file);
    file.Close();
}

如果推荐使用其他可读格式,我也愿意接受任何其他想法

非常感谢

2 个答案:

答案 0 :(得分:0)

首先,ExtremeLearningMachine没有任何公共成员序列化,所以是的:期望它是空的; XmlSerializer 仅序列化公共字段和属性。例如,尝试添加公共属性以补充您的私有字段。

其次:不要将多个片段序列化到同一个文档中。而是创建一个容器,然后序列化它。坦率地说,您可以使用ExtremeLearningMachine[]作为容器,因为您已经拥有:

var writer = new XmlSerializer(typeof(ExtremeLearningMachine[]));
string path = Path.Combine(Directory.GetCurrentDirectory(), "ElmTrain.xml");
using(var file = File.Create(path)) {
    writer.Serialize(file, elm);
}

var reader = new XmlSerializer(typeof(ExtremeLearningMachine[]));
using(var file = File.OpenRead(path)) {
    elm = (ExtremeLearningMachine[])reader.Deserialize(file);
}

答案 1 :(得分:0)

转换时请尝试以下代码:

            XmlSerializer serializer = new XmlSerializer(typeof(ExtremeLearningMachine ));
            MemoryStream memStream = new MemoryStream();
            serializer.Serialize(memStream, elm);
            FileStream file = new FileStream(folderName + "\\ElmTrain.xml", FileMode.Create, FileAccess.ReadWrite);//Provide  correct path as foldername
            memStream.WriteTo(file);
            file.Close();