简体   繁体   English

XML序列化一个类会导致XML空

[英]XML Serializing a Class results Empty XML

I'm trying to save a class in a readable format (XML). 我正在尝试以可读格式(XML)保存类。 The problem is, the resulting file only outputted as: 问题是,生成的文件仅输出为:

<?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" />

Here's my 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];
    }
}

And here is my save code: 这是我的保存代码:

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

Also my load code, in case anything wrong (I haven't tested it yet since I can't save): 还有我的加载代码,以防万一(我还无法保存,所以我尚未对其进行测试):

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

I'm also open to any other idea to save this class in other readable formats if it's recommended 如果建议的话,我也愿意采用其他可读格式保存此类的其他想法

Thank you very much 非常感谢你

Firstly, ExtremeLearningMachine doesn't have any public members to serialize, so yeah: expect it to be empty; 首先, ExtremeLearningMachine没有任何公共成员要序列化,所以是的:期望它为空; XmlSerializer only serializes public fields and properties . XmlSerializer 仅序列化公共字段和属性 Try adding public properties to complement your private fields, for example. 例如,尝试添加公共属性来补充您的私有字段。

Secondly: don't serialize multiple fragments to the same document. 其次:不要将多个片段序列化到同一文档。 Instead, create a container , and serialize that. 而是创建一个容器 ,并对其进行序列化。 Frankly, you could just use ExtremeLearningMachine[] as the container, since you already have that: 坦白说,您可以只使用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);
}

and: 和:

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

While converting try the below code: 转换时尝试以下代码:

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

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

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