Advertisement
Guest User

Untitled

a guest
May 9th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4.  
  5. namespace XmlParser
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. if (args.Length < 1)
  12. {
  13. Console.WriteLine("Please provide the path to the XML file as a command-line argument.");
  14. return;
  15. }
  16.  
  17. string filePath = args[0];
  18. int count = ParseXmlStream(filePath);
  19. Console.WriteLine("count = " + count);
  20. }
  21.  
  22. private static int ParseXmlStream(string filePath)
  23. {
  24. int count = 0;
  25. using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  26. using (XmlReader reader = XmlReader.Create(fileStream))
  27. {
  28. while (reader.Read())
  29. {
  30. if (reader.NodeType == XmlNodeType.Element && reader.Name == "location")
  31. {
  32. string locationData = reader.ReadElementContentAsString();
  33. if (locationData.Contains("Africa"))
  34. {
  35. count++;
  36. }
  37. }
  38. }
  39. }
  40.  
  41. return count;
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement