加载XML文件数据

时间:2014-04-10 15:30:17

标签: c# xml c#-4.0 xml-parsing

正在加载数据,只加载我执行保存的XML文件中的名称。

Thi是XML文件:         // trainingfaces文件

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

我只能加载名称的问题。我可以用名称加载年龄和额外信息。 我加载名称并将其放入List <string>并使其等于面label。我希望它加载年龄,以及其他信息。

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;

2 个答案:

答案 0 :(得分:1)

您是否看过内置的XML序列化/反序列化? Here is one example

答案 1 :(得分:0)

您可以尝试使用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");
            }
        }
    }