PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/HW_XML/Parsers/HW_XML_Parsers/02.ExtractingArtistsDOM/DOMParser.cs

https://github.com/bankova/Databases
C# | 46 lines | 37 code | 6 blank | 3 comment | 1 complexity | e30f9406d57e8547d54fec53a8a39d01 MD5 | raw file
  1. //Write program that extracts all different artists which are found in the
  2. //catalog.xml. For each author you should print the number of albums in the
  3. //catalogue. Use the DOM parser and a hash-table.
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Xml;
  11. namespace ExtractingArtistsDOM
  12. {
  13. class DOMParser
  14. {
  15. static void Main()
  16. {
  17. XmlDocument doc = new XmlDocument();
  18. doc.Load("../../catalogue.xml");
  19. XmlNode rootNode = doc.DocumentElement;
  20. Dictionary<string, int> artistDict = new Dictionary<string, int>();
  21. foreach (XmlNode node in rootNode.ChildNodes)
  22. {
  23. var artistName = node["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. }