Loading Data, load only the name in the XML file I did the save.

This is the

 XML

file : // The trainingfaces file

<?xml version="1.0" encoding="utf-8"?>
 <Faces_For_Training>
     <FACE>
         <NAME>john</NAME>
         <Age>25</Age>
         <informations>Student in MS university</informations>
         <FILE>face_john 25Student in MS university_905807542.jpg</FILE>
     </FACE>
     <FACE>
         <NAME>mark</NAME>
         <Age>40</Age>
         <informations>engineer ....</informations>
         <FILE>face_mark 40engineer ....._390671740.jpg</FILE>
     </FACE>
  </Faces_For_Training> 

The problem that I can load only the name .how I can load age and extra information with the name . I load the name and put it in List <string> and make it equal to face label. I want it load Age , and the other information's .

private bool LoadTrainingData(string Folder_location)
{
    if (File.Exists(Folder_location +"\\TrainedLabels.xml"))
    {
        try
        {
            //message_bar.Text = "";
            Names_List.Clear();
            Names_List_ID.Clear();
            trainingImages.Clear();
            FileStream filestream = File.OpenRead(Folder_location + "\\TrainedLabels.xml");
            long filelength = filestream.Length;
            byte[] xmlBytes = new byte[filelength];
            filestream.Read(xmlBytes, 0, (int)filelength);
            filestream.Close();

            MemoryStream xmlStream = new MemoryStream(xmlBytes);

            using (XmlReader xmlreader = XmlTextReader.Create(xmlStream))
            {
                while (xmlreader.Read())
                {
                    if (xmlreader.IsStartElement())
                    {
                        switch (xmlreader.Name)
                        {
                            case "NAME":
                                if (xmlreader.Read())
                                {
                                    Names_List_ID.Add(Names_List.Count); //0, 1, 2, 3....
                                    Names_List.Add(xmlreader.Value.Trim());
                                    NumLabels += 3;


                                }
                                break;
                            case "FILE":
                                if (xmlreader.Read())
                                {
                                    //PROBLEM HERE IF TRAININGG MOVED
                                    trainingImages.Add(new Image<Gray, byte>(Application.StartupPath + "\\TrainedFaces\\" + xmlreader.Value.Trim()));
                                }
                                break;
                           // case "Age":
                               //   if (xmlreader.Read())
                                  //{
                                  //    Age_List.Add(xmlreader.Value.Trim());


                               //   }
                                // break;
                        }
                    }
                }
            }
            ContTrain = NumLabels;

Ahh XML, it can be a stumper at first, but once you learn the tricks, it becomes easy and powerful. Let's see, where did I leave that code (I'll convert it over to match yours)

List<string> Names = new List<string>();
List<int> Ages = new List<int>();
List<string> Informations = new List<string>();
List<string> ImageFile = new List<string>();

//Load in the document, and convert it over to an XML (document needs to be a valid
//XML document for this to work)
XmlDocument myDocument = new XmlDocument();
myDocument.LoadXml(Folder_location +"\\TrainedLabels.xml");

//Select all the Nodes that are "<FACE>" and then index through each
//retriveing the values from the nodes inside.
foreach(XmlNode node in myDocument.SelectNodes("//FACE"))
{
    Names.Add(node["NAME"].InnerText);
    Ages.Add(Convert.ToInt32(node["Age"].InnerText));
    Informations.Add(node["informations"].InnerText);
    ImageFile.Add(node["FILE"].InnerText);
}

That should do it for you (if this is what you were asking), or at least get your started in the right direction (I believe I wrote this correctly, I unfortantly left the document I wrote up at work that would have been nice to compare against, but I still remember it).

You should note that retrieveing XML values like this
<field>"SOME VALUE"</field>
is done differently then if you wanted to fetch a value like
<field specialValue="SOME VALUE"/>
If you are interested in how to retrieve values like this let me, I have code laying around for that to

Edited 2 Years Ago by JOSheaIV

This article has been dead for over six months. Start a new discussion instead.