Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.00 KB | None | 0 0
  1. static void Main(string[] args)
  2.         {
  3.  
  4.             // Create an array of lines with all the lines in the people.txt
  5.             string[] personFile = File.ReadAllLines("people.txt");
  6.  
  7.             // Creation of an XmlDocument with a root node called people.
  8.             XmlDocument doc = new XmlDocument();
  9.             XmlNode peopleNode = doc.CreateNode(XmlNodeType.Element, "people", null);
  10.             doc.AppendChild(peopleNode);
  11.  
  12.             XmlNode personNode = null;
  13.             var x = 0;
  14.  
  15.             // Processing of all the rows in the personFile array / people.txt  
  16.             while (personFile[x].Substring(0, 2).Contains("P|"))
  17.             {
  18.                 // Remove the "P|" from the rows and add every string separated by the delimiter to a new array.
  19.                 string[] personArr = personFile[x].Remove(0, 2).Split('|');
  20.  
  21.                 // [P] Person element (firstname and lastname)
  22.                 personNode = doc.CreateNode(XmlNodeType.Element, "person", null);
  23.                 if (doc.DocumentElement != null) doc.DocumentElement.AppendChild(personNode);
  24.  
  25.                 XmlNode firstnamePersonNode = doc.CreateNode(XmlNodeType.Element, "firstname", null);
  26.                 personNode.AppendChild(firstnamePersonNode);
  27.                 XmlNode lastnamePersonNode = doc.CreateNode(XmlNodeType.Element, "lastname", null);
  28.                 personNode.AppendChild(lastnamePersonNode);
  29.  
  30.                 firstnamePersonNode.InnerText = personArr[0];
  31.                 lastnamePersonNode.InnerText = personArr[1];
  32.  
  33.                 if (x < personFile.Length - 1)
  34.                 {
  35.                     x++; // inkrements to the next row
  36.                 }
  37.  
  38.                 // Checking for Telephone
  39.                 if (personFile[x].Substring(0, 2).Contains("T|"))
  40.                 {
  41.                     string[] teleArr = personFile[x].Remove(0, 2).Split('|');
  42.  
  43.                     // [T] Telephone element (mobile and lastname)
  44.                     XmlNode phoneNode = doc.CreateNode(XmlNodeType.Element, "phone", null);
  45.                     personNode.AppendChild(phoneNode);
  46.  
  47.                     XmlNode mobilePhoneNode = doc.CreateNode(XmlNodeType.Element, "mobile", null);
  48.                     phoneNode.AppendChild(mobilePhoneNode);
  49.                     XmlNode fixedNumberPhoneNode = doc.CreateNode(XmlNodeType.Element, "fixednumber", null);
  50.                     phoneNode.AppendChild(fixedNumberPhoneNode);
  51.  
  52.                     mobilePhoneNode.InnerText = teleArr[0];
  53.                     fixedNumberPhoneNode.InnerText = teleArr[1];
  54.  
  55.                     if (x < personFile.Length - 1)
  56.                     {
  57.                         x++; // inkrements to the next row
  58.                     }
  59.                 }
  60.  
  61.                 // Checking for Address
  62.                 if (personFile[x].Substring(0, 2).Contains("A|"))
  63.                 {
  64.                     string[] addressArr = personFile[x].Remove(0, 2).Split('|');
  65.  
  66.                     //[A] Address element (Street, city and lastname)
  67.                     XmlNode addressNode = doc.CreateNode(XmlNodeType.Element, "address", null);
  68.                     if (personNode != null) personNode.AppendChild(addressNode);
  69.  
  70.                     XmlNode streetAddressNode = doc.CreateNode(XmlNodeType.Element, "street", null);
  71.                     addressNode.AppendChild(streetAddressNode);
  72.                     XmlNode cityAddressNode = doc.CreateNode(XmlNodeType.Element, "city", null);
  73.                     addressNode.AppendChild(cityAddressNode);
  74.                     XmlNode postalcodeAddressNode = doc.CreateNode(XmlNodeType.Element, "postalcode", null);
  75.                     addressNode.AppendChild(postalcodeAddressNode);
  76.  
  77.                     if (personArr.Length == 2)
  78.                     {
  79.                         streetAddressNode.InnerText = addressArr[0];
  80.                         cityAddressNode.InnerText = addressArr[1];
  81.                     }
  82.                     else if (personArr.Length == 3)
  83.                     {
  84.                         streetAddressNode.InnerText = addressArr[0];
  85.                         cityAddressNode.InnerText = addressArr[1];
  86.                         postalcodeAddressNode.InnerText = addressArr[2];
  87.                     }
  88.  
  89.                     if (x < personFile.Length - 1)
  90.                     {
  91.                         x++; // inkrements to the next row
  92.                     }
  93.  
  94.                     // Checking for potentially several family members
  95.                     while (personFile[x].Substring(0, 2).Contains("F|"))
  96.                     {
  97.                         string[] familyMemberArr = personFile[x].Remove(0, 2).Split('|');
  98.  
  99.                         //[A] Address element (Street, city and lastname)
  100.                         XmlNode familyNode = doc.CreateNode(XmlNodeType.Element, "family", null);
  101.                         if (personNode != null) personNode.AppendChild(familyNode);
  102.  
  103.                         XmlNode nameFamilyNode = doc.CreateNode(XmlNodeType.Element, "name", null);
  104.                         familyNode.AppendChild(nameFamilyNode);
  105.                         XmlNode birthDateFamilyNode = doc.CreateNode(XmlNodeType.Element, "born", null);
  106.                         familyNode.AppendChild(birthDateFamilyNode);
  107.  
  108.                         nameFamilyNode.InnerText = familyMemberArr[0];
  109.                         birthDateFamilyNode.InnerText = familyMemberArr[1];
  110.  
  111.                         if (x < personFile.Length - 1)
  112.                         {
  113.                             x++; // inkrements to the next row
  114.                         }
  115.  
  116.                         // Checking for Telephone
  117.                         if (personFile[x].Substring(0, 2).Contains("T|"))
  118.                         {
  119.                             string[] teleFamilyArr = personFile[x].Remove(0, 2).Split('|');
  120.  
  121.                             // [T] Telephone element (mobile and lastname)
  122.                             XmlNode phoneFamilyNode = doc.CreateNode(XmlNodeType.Element, "phone", null);
  123.                             if (familyNode != null) familyNode.AppendChild(phoneFamilyNode);
  124.  
  125.                             XmlNode mobilePhoneFamilyNode = doc.CreateNode(XmlNodeType.Element, "mobile", null);
  126.                             phoneFamilyNode.AppendChild(mobilePhoneFamilyNode);
  127.                             XmlNode fixedNumberFamilyPhoneNode = doc.CreateNode(XmlNodeType.Element, "fixednumber", null);
  128.                             phoneFamilyNode.AppendChild(fixedNumberFamilyPhoneNode);
  129.  
  130.                             mobilePhoneFamilyNode.InnerText = teleFamilyArr[0];
  131.                             fixedNumberFamilyPhoneNode.InnerText = teleFamilyArr[1];
  132.  
  133.                             if (x < personFile.Length - 1)
  134.                             {
  135.                                 x++; // inkrements to the next row
  136.                             }
  137.                         }
  138.  
  139.                         // Checking for Address
  140.                         if (personFile[x].Substring(0, 2).Contains("A|"))
  141.                         {
  142.                             string[] addressFamilyArr = personFile[x].Remove(0, 2).Split('|');
  143.  
  144.                             //[A] Address element (Street, city and lastname)
  145.                             XmlNode addressFamilyNode = doc.CreateNode(XmlNodeType.Element, "address", null);
  146.                             if (familyNode != null) familyNode.AppendChild(addressFamilyNode);
  147.  
  148.                             XmlNode streetAddressFamilyNode = doc.CreateNode(XmlNodeType.Element, "street", null);
  149.                             addressFamilyNode.AppendChild(streetAddressFamilyNode);
  150.                             XmlNode cityAddressFamilyNode = doc.CreateNode(XmlNodeType.Element, "city", null);
  151.                             addressFamilyNode.AppendChild(cityAddressFamilyNode);
  152.                             XmlNode postalCodeAddressFamilyNode = doc.CreateNode(XmlNodeType.Element, "postalcode", null);
  153.                             addressFamilyNode.AppendChild(postalCodeAddressFamilyNode);
  154.  
  155.                             if (addressFamilyArr.Length == 2)
  156.                             {
  157.                                 streetAddressFamilyNode.InnerText = addressFamilyArr[0];
  158.                                 cityAddressFamilyNode.InnerText = addressFamilyArr[1];
  159.                             }
  160.                             else if (addressFamilyArr.Length == 3)
  161.                             {
  162.                                 streetAddressFamilyNode.InnerText = addressFamilyArr[0];
  163.                                 cityAddressFamilyNode.InnerText = addressFamilyArr[1];
  164.                                 postalCodeAddressFamilyNode.InnerText = addressFamilyArr[2];
  165.                             }
  166.  
  167.                             if (x < personFile.Length - 1)
  168.                             {
  169.                                 x++; // inkrements to the next row
  170.                             }
  171.                         }
  172.                     }
  173.                 }
  174.             }
  175.             doc.Save("people.xml"); // save everything in the a file called people.xml
  176.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement