PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/HW_XML/Parsers/HW_XML_Parsers/03.XPath/XPath.cs

https://github.com/bankova/Databases
C# | 45 lines | 39 code | 5 blank | 1 comment | 1 complexity | 9e3b7e9f57b5ed93f6d10ac02984f1d7 MD5 | raw file
  1. //Implement the previous using XPath.
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Xml;
  9. namespace ExtractingArtistsDOM
  10. {
  11. class XPath
  12. {
  13. static void Main()
  14. {
  15. XmlDocument xmlDoc = new XmlDocument();
  16. xmlDoc.Load("../../catalogue.xml");
  17. XmlNode rootNode = xmlDoc.DocumentElement;
  18. Dictionary<string, int> artistDict = new Dictionary<string, int>();
  19. string xPathQuery = "catalogue/album";
  20. XmlNodeList artistList = xmlDoc.SelectNodes(xPathQuery);
  21. foreach (XmlNode artist in artistList)
  22. {
  23. string artistName = artist.SelectSingleNode("artist").InnerText;
  24. if (artistDict.ContainsKey(artistName))
  25. {
  26. artistDict[artistName]++;
  27. }
  28. else
  29. {
  30. artistDict.Add(artistName, 1);
  31. }
  32. }
  33. foreach (var item in artistDict)
  34. {
  35. Console.WriteLine("Artist Name:{0,-25}Number of Albums {1,10}",
  36. item.Key, item.Value);
  37. }
  38. }
  39. }
  40. }