PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/HW_XML/Parsers/HW_XML_Parsers/08.Album/Album.cs

https://github.com/bankova/Databases
C# | 60 lines | 50 code | 7 blank | 3 comment | 8 complexity | 13495a1547a29e7b5b394c338c0cc909 MD5 | raw file
  1. //Write a program, which (using XmlReader and XmlWriter) reads the file
  2. //catalog.xml and creates the file album.xml, in which stores in appropriate
  3. //way the names of all albums and their authors.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Xml;
  10. class Album
  11. {
  12. static void Main()
  13. {
  14. string fileName = "../../album.xml";
  15. Encoding encoding = Encoding.GetEncoding("windows-1251");
  16. XmlTextWriter writer = new XmlTextWriter(fileName, encoding);
  17. using (writer)
  18. {
  19. writer.WriteStartDocument();
  20. writer.Formatting = Formatting.Indented;
  21. writer.IndentChar = '\t';
  22. writer.Indentation = 1;
  23. writer.WriteStartElement("albums");
  24. string name = string.Empty;
  25. using (XmlReader reader = XmlReader.Create("../../catalogue.xml"))
  26. {
  27. while (reader.Read())
  28. {
  29. if ((reader.NodeType == XmlNodeType.Element) &&
  30. (reader.Name == "name"))
  31. {
  32. name = reader.ReadElementString();
  33. }
  34. else if ((reader.NodeType == XmlNodeType.Element) &&
  35. (reader.Name == "artist"))
  36. {
  37. string artist = reader.ReadElementString();
  38. WriteBook(writer, name, artist);
  39. }
  40. }
  41. }
  42. writer.WriteEndDocument();
  43. Console.WriteLine("Document {0} was created.", fileName);
  44. }
  45. }
  46. private static void WriteBook(XmlWriter writer, string name, string artist)
  47. {
  48. writer.WriteStartElement("album");
  49. writer.WriteElementString("name", name);
  50. writer.WriteElementString("artist", artist);
  51. writer.WriteEndElement();
  52. }
  53. }