Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- // Create an array of lines with all the lines in the people.txt
- string[] personFile = File.ReadAllLines("people.txt");
- // Creation of an XmlDocument with a root node called people.
- XmlDocument doc = new XmlDocument();
- XmlNode peopleNode = doc.CreateNode(XmlNodeType.Element, "people", null);
- doc.AppendChild(peopleNode);
- XmlNode personNode = null;
- var x = 0;
- // Processing of all the rows in the personFile array / people.txt
- while (personFile[x].Substring(0, 2).Contains("P|"))
- {
- // Remove the "P|" from the rows and add every string separated by the delimiter to a new array.
- string[] personArr = personFile[x].Remove(0, 2).Split('|');
- // [P] Person element (firstname and lastname)
- personNode = doc.CreateNode(XmlNodeType.Element, "person", null);
- if (doc.DocumentElement != null) doc.DocumentElement.AppendChild(personNode);
- XmlNode firstnamePersonNode = doc.CreateNode(XmlNodeType.Element, "firstname", null);
- personNode.AppendChild(firstnamePersonNode);
- XmlNode lastnamePersonNode = doc.CreateNode(XmlNodeType.Element, "lastname", null);
- personNode.AppendChild(lastnamePersonNode);
- firstnamePersonNode.InnerText = personArr[0];
- lastnamePersonNode.InnerText = personArr[1];
- if (x < personFile.Length - 1)
- {
- x++; // inkrements to the next row
- }
- // Checking for Telephone
- if (personFile[x].Substring(0, 2).Contains("T|"))
- {
- string[] teleArr = personFile[x].Remove(0, 2).Split('|');
- // [T] Telephone element (mobile and lastname)
- XmlNode phoneNode = doc.CreateNode(XmlNodeType.Element, "phone", null);
- personNode.AppendChild(phoneNode);
- XmlNode mobilePhoneNode = doc.CreateNode(XmlNodeType.Element, "mobile", null);
- phoneNode.AppendChild(mobilePhoneNode);
- XmlNode fixedNumberPhoneNode = doc.CreateNode(XmlNodeType.Element, "fixednumber", null);
- phoneNode.AppendChild(fixedNumberPhoneNode);
- mobilePhoneNode.InnerText = teleArr[0];
- fixedNumberPhoneNode.InnerText = teleArr[1];
- if (x < personFile.Length - 1)
- {
- x++; // inkrements to the next row
- }
- }
- // Checking for Address
- if (personFile[x].Substring(0, 2).Contains("A|"))
- {
- string[] addressArr = personFile[x].Remove(0, 2).Split('|');
- //[A] Address element (Street, city and lastname)
- XmlNode addressNode = doc.CreateNode(XmlNodeType.Element, "address", null);
- if (personNode != null) personNode.AppendChild(addressNode);
- XmlNode streetAddressNode = doc.CreateNode(XmlNodeType.Element, "street", null);
- addressNode.AppendChild(streetAddressNode);
- XmlNode cityAddressNode = doc.CreateNode(XmlNodeType.Element, "city", null);
- addressNode.AppendChild(cityAddressNode);
- XmlNode postalcodeAddressNode = doc.CreateNode(XmlNodeType.Element, "postalcode", null);
- addressNode.AppendChild(postalcodeAddressNode);
- if (personArr.Length == 2)
- {
- streetAddressNode.InnerText = addressArr[0];
- cityAddressNode.InnerText = addressArr[1];
- }
- else if (personArr.Length == 3)
- {
- streetAddressNode.InnerText = addressArr[0];
- cityAddressNode.InnerText = addressArr[1];
- postalcodeAddressNode.InnerText = addressArr[2];
- }
- if (x < personFile.Length - 1)
- {
- x++; // inkrements to the next row
- }
- // Checking for potentially several family members
- while (personFile[x].Substring(0, 2).Contains("F|"))
- {
- string[] familyMemberArr = personFile[x].Remove(0, 2).Split('|');
- //[A] Address element (Street, city and lastname)
- XmlNode familyNode = doc.CreateNode(XmlNodeType.Element, "family", null);
- if (personNode != null) personNode.AppendChild(familyNode);
- XmlNode nameFamilyNode = doc.CreateNode(XmlNodeType.Element, "name", null);
- familyNode.AppendChild(nameFamilyNode);
- XmlNode birthDateFamilyNode = doc.CreateNode(XmlNodeType.Element, "born", null);
- familyNode.AppendChild(birthDateFamilyNode);
- nameFamilyNode.InnerText = familyMemberArr[0];
- birthDateFamilyNode.InnerText = familyMemberArr[1];
- if (x < personFile.Length - 1)
- {
- x++; // inkrements to the next row
- }
- // Checking for Telephone
- if (personFile[x].Substring(0, 2).Contains("T|"))
- {
- string[] teleFamilyArr = personFile[x].Remove(0, 2).Split('|');
- // [T] Telephone element (mobile and lastname)
- XmlNode phoneFamilyNode = doc.CreateNode(XmlNodeType.Element, "phone", null);
- if (familyNode != null) familyNode.AppendChild(phoneFamilyNode);
- XmlNode mobilePhoneFamilyNode = doc.CreateNode(XmlNodeType.Element, "mobile", null);
- phoneFamilyNode.AppendChild(mobilePhoneFamilyNode);
- XmlNode fixedNumberFamilyPhoneNode = doc.CreateNode(XmlNodeType.Element, "fixednumber", null);
- phoneFamilyNode.AppendChild(fixedNumberFamilyPhoneNode);
- mobilePhoneFamilyNode.InnerText = teleFamilyArr[0];
- fixedNumberFamilyPhoneNode.InnerText = teleFamilyArr[1];
- if (x < personFile.Length - 1)
- {
- x++; // inkrements to the next row
- }
- }
- // Checking for Address
- if (personFile[x].Substring(0, 2).Contains("A|"))
- {
- string[] addressFamilyArr = personFile[x].Remove(0, 2).Split('|');
- //[A] Address element (Street, city and lastname)
- XmlNode addressFamilyNode = doc.CreateNode(XmlNodeType.Element, "address", null);
- if (familyNode != null) familyNode.AppendChild(addressFamilyNode);
- XmlNode streetAddressFamilyNode = doc.CreateNode(XmlNodeType.Element, "street", null);
- addressFamilyNode.AppendChild(streetAddressFamilyNode);
- XmlNode cityAddressFamilyNode = doc.CreateNode(XmlNodeType.Element, "city", null);
- addressFamilyNode.AppendChild(cityAddressFamilyNode);
- XmlNode postalCodeAddressFamilyNode = doc.CreateNode(XmlNodeType.Element, "postalcode", null);
- addressFamilyNode.AppendChild(postalCodeAddressFamilyNode);
- if (addressFamilyArr.Length == 2)
- {
- streetAddressFamilyNode.InnerText = addressFamilyArr[0];
- cityAddressFamilyNode.InnerText = addressFamilyArr[1];
- }
- else if (addressFamilyArr.Length == 3)
- {
- streetAddressFamilyNode.InnerText = addressFamilyArr[0];
- cityAddressFamilyNode.InnerText = addressFamilyArr[1];
- postalCodeAddressFamilyNode.InnerText = addressFamilyArr[2];
- }
- if (x < personFile.Length - 1)
- {
- x++; // inkrements to the next row
- }
- }
- }
- }
- }
- doc.Save("people.xml"); // save everything in the a file called people.xml
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement