Advertisement
olegstankoptev

Untitled

Apr 13th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using System.Xml.Serialization;
  5.  
  6. class Methods
  7. {
  8. internal static XmlDocument GetDocument(string company, List<string> persons)
  9. {
  10. List<Person> people = Person.CreateList(persons);
  11.  
  12. XmlDocument doc = new XmlDocument();
  13. XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
  14. doc.AppendChild(declaration);
  15. XmlElement root = doc.CreateElement("company");
  16. XmlAttribute attribute = doc.CreateAttribute("name");
  17. attribute.Value = company;
  18. root.Attributes.Append(attribute);
  19. doc.AppendChild(root);
  20.  
  21. foreach (var person in people)
  22. PrintPersonInfo(doc, person, ref root);
  23.  
  24. return doc;
  25. }
  26.  
  27. private static void PrintPersonInfo(XmlDocument doc, Person person, ref XmlElement root)
  28. {
  29. XmlElement userNode;
  30. XmlAttribute attribute;
  31. userNode = doc.CreateElement("person");
  32. attribute = doc.CreateAttribute("id");
  33. attribute.Value = person.Id.ToString();
  34. userNode.Attributes.Append(attribute);
  35. attribute = doc.CreateAttribute("name");
  36. attribute.Value = person.Name;
  37. userNode.Attributes.Append(attribute);
  38. attribute = doc.CreateAttribute("position");
  39. attribute.Value = person.Job;
  40. userNode.Attributes.Append(attribute);
  41. if (person.Employees != null)
  42. {
  43. foreach (var pers in person.Employees)
  44. {
  45. PrintPersonInfo(doc, pers, ref userNode);
  46. }
  47. }
  48. root.AppendChild(userNode);
  49. }
  50. }
  51. [Serializable]
  52. public class Person
  53. {
  54. [XmlAttribute(AttributeName = "person id")]
  55. public int Id { get; set; }
  56.  
  57. public List<Person> Employees { get; set; }
  58. [XmlAttribute(AttributeName = "position")]
  59. public string Job { get; set; }
  60. [XmlAttribute(AttributeName = "name")]
  61. public string Name { get; set; }
  62. public static List<Person> CreateList(List<string> persons)
  63. {
  64. List<Person> bosses = new List<Person>();
  65. List<Person> people = new List<Person>();
  66. foreach (var line in persons)
  67. {
  68. string[] info = line.Split('\t');
  69. int id = int.Parse(info[0]);
  70. int bossId = int.Parse(info[1]);
  71. string job = info[2];
  72. string name = info[3];
  73.  
  74. if (id == bossId)
  75. {
  76. Person person = new Person()
  77. {
  78. Id = id,
  79. Employees = new List<Person>(),
  80. Job = job,
  81. Name = name
  82. };
  83. people.Add(person);
  84. bosses.Add(person);
  85. }
  86. else
  87. {
  88. Person boss = people.Find(x => x.Id == bossId);
  89. Person person = new Person()
  90. {
  91. Id = id,
  92. Employees = new List<Person>(),
  93. Job = job,
  94. Name = name
  95. };
  96. people.Add(person);
  97. boss.Employees.Add(person);
  98. }
  99. }
  100. return bosses;
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement