gpt4 book ai didi

c# - XML 序列化类结果为空 XML

转载 作者:太空宇宙 更新时间:2023-11-03 15:19:29 24 4
gpt4 key购买 nike

我正在尝试以可读格式 (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" />

这是我的类(class):

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

如果推荐,我也愿意接受以其他可读格式保存此类的任何其他想法

非常感谢

最佳答案

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

其次:不要将多个片段序列化到同一个文档。相反,创建一个容器,并将其序列化。坦率地说,您可以只使用 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);
}

关于c# - XML 序列化类结果为空 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37808231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com