Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Xml;
- using System.Xml.Serialization;
- class Methods
- {
- internal static XmlDocument GetDocument(string company, List<string> persons)
- {
- List<Person> people = Person.CreateList(persons);
- XmlDocument doc = new XmlDocument();
- XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
- doc.AppendChild(declaration);
- XmlElement root = doc.CreateElement("company");
- XmlAttribute attribute = doc.CreateAttribute("name");
- attribute.Value = company;
- root.Attributes.Append(attribute);
- doc.AppendChild(root);
- foreach (var person in people)
- PrintPersonInfo(doc, person, ref root);
- return doc;
- }
- private static void PrintPersonInfo(XmlDocument doc, Person person, ref XmlElement root)
- {
- XmlElement userNode;
- XmlAttribute attribute;
- userNode = doc.CreateElement("person");
- attribute = doc.CreateAttribute("id");
- attribute.Value = person.Id.ToString();
- userNode.Attributes.Append(attribute);
- attribute = doc.CreateAttribute("name");
- attribute.Value = person.Name;
- userNode.Attributes.Append(attribute);
- attribute = doc.CreateAttribute("position");
- attribute.Value = person.Job;
- userNode.Attributes.Append(attribute);
- if (person.Employees != null)
- {
- foreach (var pers in person.Employees)
- {
- PrintPersonInfo(doc, pers, ref userNode);
- }
- }
- root.AppendChild(userNode);
- }
- }
- [Serializable]
- public class Person
- {
- [XmlAttribute(AttributeName = "person id")]
- public int Id { get; set; }
- public List<Person> Employees { get; set; }
- [XmlAttribute(AttributeName = "position")]
- public string Job { get; set; }
- [XmlAttribute(AttributeName = "name")]
- public string Name { get; set; }
- public static List<Person> CreateList(List<string> persons)
- {
- List<Person> bosses = new List<Person>();
- List<Person> people = new List<Person>();
- foreach (var line in persons)
- {
- string[] info = line.Split('\t');
- int id = int.Parse(info[0]);
- int bossId = int.Parse(info[1]);
- string job = info[2];
- string name = info[3];
- if (id == bossId)
- {
- Person person = new Person()
- {
- Id = id,
- Employees = new List<Person>(),
- Job = job,
- Name = name
- };
- people.Add(person);
- bosses.Add(person);
- }
- else
- {
- Person boss = people.Find(x => x.Id == bossId);
- Person person = new Person()
- {
- Id = id,
- Employees = new List<Person>(),
- Job = job,
- Name = name
- };
- people.Add(person);
- boss.Employees.Add(person);
- }
- }
- return bosses;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement