There is an error in XML document (6, 14)
При попытке прочитать содержимое файла, выдает ошибку: "There is an error in XML document (6, 14)."
public struct Aeroflot
{
public string Destination;
public string Number;
public string PlaneModel;
public override string ToString()
{
return string.Format($"{Destination} {Number} {PlaneModel}");
}
public void Vvid()
{
Console.WriteLine("Input destination");
Destination = Console.ReadLine();
Console.WriteLine("Input flight number");
Number = Console.ReadLine();
Console.WriteLine("Input plane model");
PlaneModel = Console.ReadLine();
}
}
class Program
{
static void GetValues(Aeroflot[] Flights)
{
for (int i = 0; i < NUMBER_OF_FLIGHTS; i++)
{
Aeroflot flight = new Aeroflot();
Console.Write("Input destination: ");
flight.Destination = Console.ReadLine();
Console.Write("Input flight number: ");
flight.Number = Console.ReadLine();
Console.Write("Input plane model: ");
flight.PlaneModel = Console.ReadLine();
Flights[i] = flight;
}
}
const int NUMBER_OF_FLIGHTS = 2;
public static void Main(string[] args)
{
try
{
Aeroflot[] Flights = new Aeroflot[NUMBER_OF_FLIGHTS];
//GetValues(Flights);
for (int i = 0; i < NUMBER_OF_FLIGHTS; i++)
{
Flights[i].Vvid();
}
XmlSerializer xmlSerialaizer = new XmlSerializer(typeof(Aeroflot));
using (FileStream fw = new FileStream("output.xml", FileMode.Create))
{
for (int i = 0; i < 1; i++)
xmlSerialaizer.Serialize(fw, Flights[i]);
}
Aeroflot[] bludaToRead = new Aeroflot[NUMBER_OF_FLIGHTS];
using (FileStream fr = new FileStream("output.xml", FileMode.Open))
{
for (int i = 0; i < NUMBER_OF_FLIGHTS; i++)
bludaToRead[i] = (Aeroflot)xmlSerialaizer.Deserialize(fr);
}
int matches = 0;
string DestinationToCompare = Console.ReadLine();
for (int i = 0; i < Flights.Length; i++)
{
if (Flights[i].Destination == DestinationToCompare)
{
Console.WriteLine(Flights[i].Number + " " + Flights[i].PlaneModel + " " + Flights[i].Destination);
matches++;
}
}
if (matches == 0)
{
Console.WriteLine("There is no matches");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
Ответы (1 шт):
Автор решения: VlaDOS
→ Ссылка
public struct Aeroflot
{
public string Destination { get; set; }
public string Number { get; set; }
public string PlaneModel { get; set; }
public override string ToString()
{
return string.Format($"Destination: {Destination}. Number: {Number}. Plane model: {PlaneModel}");
}
}
class Program
{
const int NUMBER_OF_FLIGHTS = 3;
static void GetValues(Aeroflot[] Flights)
{
for (int i = 0; i < NUMBER_OF_FLIGHTS; i++)
{
Aeroflot flight = new Aeroflot();
Console.Write("\n Input destination: ");
flight.Destination = Console.ReadLine();
Console.Write(" Input flight number: ");
flight.Number = Console.ReadLine();
Console.Write(" Input plane model: ");
flight.PlaneModel = Console.ReadLine();
Flights[i] = flight;
}
}
public static void Line()
{
Console.WriteLine("----------------------------");
}
public static void Main(string[] args)
{
try
{
Aeroflot[] Flights = new Aeroflot[NUMBER_OF_FLIGHTS];
Line();
Console.WriteLine(" INPUT THE DATA:");
GetValues(Flights);
List<Aeroflot> list = new List<Aeroflot>();
list.AddRange(Flights);
XmlSerializer xmlSerialaizer = new XmlSerializer(typeof(List<Aeroflot>));
string pathXml = "file2.xml";
using (FileStream fw = new FileStream(pathXml, FileMode.Create))
{
xmlSerialaizer.Serialize(fw, list);
}
Line();
Console.WriteLine(" READ FILE .xml:\n");
List<Aeroflot> bludaToRead = new List<Aeroflot>();
using (FileStream fr = new FileStream(pathXml, FileMode.Open))
{
bludaToRead = (List<Aeroflot>)xmlSerialaizer.Deserialize(fr);
foreach (Aeroflot i in bludaToRead)
{
Console.WriteLine($" Destination: {i.Destination}. Number: {i.Number}. Plane model: {i.PlaneModel}");
}
}
Line();
Console.Write("\n Input destination: ");
string DestinationToCompare = Console.ReadLine();
int matches = 0;
for (int i = 0; i < Flights.Length; i++)
{
if (Flights[i].Destination == DestinationToCompare)
{
Console.WriteLine($" Number: {Flights[i].Number}\n Plane Model: {Flights[i].PlaneModel}");
matches++;
}
}
if (matches == 0)
{
Console.WriteLine("\n THERE IS NO MATCHES!!");
}
Line();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
