简体   繁体   中英

Loading XML file data

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

Thi 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;

Have you looked at the built-in XML serialization/deserialization? Here is one example .

You could try using linq to xml

    private void LoadTrainingData(string folderLocation)
    {
        var filePath = folderLocation + "\\TrainedLabels.xml";

        if (File.Exists(filePath))
        {
            var doc = XDocument.Load(folderLocation + "\\TrainedLabels.xml");
            var items = doc.Root.Elements("FACE");
            foreach (var item in items)
            {
                var names = item.Elements("NAME");
                var ages = item.Elements("Age");
                var faces = item.Elements("FACE");
            }
        }
    }

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