0

Делая курсовую работу, столкнулся с проблемой, описанной в заголовке. Пробовал менять типы данных у переменных и редактировать сам XML-файл - никак не помогло. Скорее всего, упускаю что-то очевидное. Очень бы хотел узнать что именно не так

Код:

using System;
using System.IO;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace ConsoleApp1
{
  [Serializable]
  abstract public class Block
  {
    //urok id
    protected string id;
    public string ID
    {
      get { return id; }
      set
      {
        id = value;
      }
    }

    //URLs
    protected string url;
    public string URL
    {
      get
      {
        return url;
      }
      set
      {
        url = value;
      }
    }

    protected string status;
    public string Status
    {
      get
      {
        return status;
      }
      set
      {
        status = value;
      }
    }
    public Block() { }
    public Block(string id, string url, string status)
    {
      this.id = id;
      this.url = url;
      this.status = status;
    }

  }

  [Serializable]
  public class Urok : Block
  {
    public Urok() { }
    public Urok(string id, string url, string status)
    {
      this.id = id;
      this.url = url;
      this.status = status;
    }

  }

  [Serializable]
  public class Test : Block
  {
    public string otv;
    public string Otv
    {
      get
      {
        return otv;
      }
      set
      {
        otv = value;
      }
    }

    public Test() { }
    public Test(string id, string url, string status, string otv):base(id, url, status)
    {
      this.id = id;
      this.url = url;
      this.status = status;
      this.otv = otv;
    }

  }

  class Program
  {
    static void Main(string[] args)
    {
      List<Test> tests = new List<Test>();

      XmlSerializer xmlSerializer = new XmlSerializer(typeof(Test));

      try
      {
        using (FileStream file = new FileStream("tests.xml", FileMode.OpenOrCreate))
        {
          tests = xmlSerializer.Deserialize(file) as List<Test>;
        }
      }
      catch(Exception e)
      {
        Console.WriteLine(e);
      }


      foreach(Test test in tests)
      {
        Console.WriteLine(test.ID);
        Console.WriteLine(test.Otv);
        Console.WriteLine(test.URL);
        Console.WriteLine(test.Status);
      }

    }
  }
}

XML-файл:

<?xml version="1.0"?>
<ArrayOfTest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Test>
    <ID>1</ID>
    <URL>C:\Users\user\OneDrive\Рабочий стол\Kurs\code\drozdov\tests_pics\1</URL>
    <Otv>26|6|22|20|15</Otv>
    <Status>unfinished</Status>
  </Test>
  <Test>
    <ID>2</ID>
    <URL>C:\Users\user\OneDrive\Рабочий стол\Kurs\code\drozdov\tests_pics\2</URL>
    <Otv>wzyx|zwxy|zyx|yxz|31</Otv>
    <Status>unfinished</Status>
  </Test>
  <Test>
    <ID>3</ID>
    <URL>C:\Users\user\OneDrive\Рабочий стол\Kurs\code\drozdov\tests_pics\3</URL>
    <Otv>4|001|36714|22|14</Otv>
    <Status>unfinished</Status>
  </Test>
  <Test>
    <ID>4</ID>
    <URL>C:\Users\user\OneDrive\Рабочий стол\Kurs\code\drozdov\tests_pics\4</URL>
    <Otv>21221|168|58|назад5|2221</Otv>
    <Status>unfinished</Status>
  </Test>
  <Test>
    <ID>5</ID>
    <URL>C:\Users\user\OneDrive\Рабочий стол\Kurs\code\drozdov\tests_pics\5</URL>
    <Otv>165|98|16|120|15</Otv>
    <Status>unfinished</Status>
  </Test>
</ArrayOfTest>

Прошу вас ткнуть меня носом в проблему)

3
  • 1
    Вы создаёте сериализатор для одного типа typeof(Test), а получить пытаетесь список List<Test>. 7 мая 2022 в 11:43
  • Замечание: FileMode.OpenOrCreate - открывает файл, если он есть, или создаёт новый, если его нет. Нет смысла создавать новый файл, потому что из пустого файла нечего десериализовать. Используйте FileMode.Open - это будет семантически правильно. 7 мая 2022 в 11:46
  • Атрибут [Serializable] не нужен. Можете убрать его со всех классов. 7 мая 2022 в 11:48

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.