简体   繁体   中英

XML Serializing a Class results Empty XML

I'm trying to save a class in a readable format (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; XmlSerializer only serializes public fields and properties . 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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