fork download
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5.  
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. string inputFilePath = "input.txt"; // Имя существующего текстового файла
  11. string outputXmlPath = "output.xml"; // Имя создаваемого XML-документа
  12.  
  13. if (!File.Exists(inputFilePath))
  14. {
  15. Console.WriteLine("Файл не найден!");
  16. return;
  17. }
  18.  
  19. // Читаем все строки из файла
  20. var lines = File.ReadAllLines(inputFilePath);
  21.  
  22. // Создаём XML-документ
  23. XElement root = new XElement("root",
  24. lines.Select((line, index) => new XElement("line", new XAttribute("num", index + 1), line))
  25. );
  26.  
  27. // Сохраняем XML в файл
  28. root.Save(outputXmlPath);
  29.  
  30. Console.WriteLine($"XML-файл '{outputXmlPath}' успешно создан.");
  31. }
  32. }
Success #stdin #stdout 0.07s 31228KB
stdin
Standard input is empty
stdout
Файл не найден!