Advertisement
Dido09

Untitled

Nov 25th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. using System.Reflection.Emit;
  2. using System.Xml;
  3.  
  4. namespace PRS_lab1
  5. {
  6. internal class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. while (true)
  11. {
  12. Console.WriteLine("\nChoose an action:");
  13. Console.WriteLine("1. Read XML data (currency rates)");
  14. Console.WriteLine("2. Write XML data (user details)");
  15. Console.WriteLine("3. Exit");
  16. string input = Console.ReadLine();
  17.  
  18. switch (input)
  19. {
  20. case "1":
  21. ReadXMLData();
  22. break;
  23. case "2":
  24. WriteXMLData();
  25. break;
  26. case "3":
  27. Console.WriteLine("Existing...");
  28. return;
  29. default:
  30. Console.WriteLine("Invalid choice, please try again.");
  31. break;
  32.  
  33. }
  34. }
  35. }
  36. static void ReadXMLData()
  37. {
  38. try
  39. {
  40. XmlDocument xmlDoc = new XmlDocument();
  41. xmlDoc.Load("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
  42.  
  43. foreach (XmlNode xmlNode in xmlDoc.DocumentElement.ChildNodes[2].ChildNodes[0].ChildNodes)
  44. {
  45. string currency = xmlNode.Attributes["currency"].Value;
  46. string rate = xmlNode.Attributes["rate"].Value;
  47. Console.WriteLine($"{currency}: {rate}");
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. Console.WriteLine($"Error reading XML data: {ex.Message}");
  53. }
  54. }
  55.  
  56. static void WriteXMLData()
  57. {
  58. try
  59. {
  60. Console.Write("Enter user name: ");
  61. string name = Console.ReadLine();
  62.  
  63. Console.Write("Enter user age: ");
  64. string age = Console.ReadLine();
  65.  
  66. XmlDocument xmlDoc = new XmlDocument();
  67. XmlNode rootNode = xmlDoc.CreateElement("users");
  68. xmlDoc.AppendChild(rootNode);
  69.  
  70. XmlNode userNode = xmlDoc.CreateElement("user");
  71. XmlAttribute attribute = xmlDoc.CreateAttribute("age");
  72. attribute.Value = age;
  73. userNode.Attributes.Append(attribute);
  74. userNode.InnerText = name;
  75. rootNode.AppendChild(userNode);
  76.  
  77. xmlDoc.Save(@"C:\Users\Student\Desktop\PRS-Lab1\TestXML.xml");
  78. Console.WriteLine("User data saved to XML file.");
  79. }
  80. catch (Exception ex)
  81. {
  82. Console.WriteLine($"Error writing XML data: {ex.Message}");
  83. }
  84. }
  85. }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement