Convert TXT to XML, using C # code [closed]

-3

I want to put the data below that is in a text file ( txt ) to XML , what is the C# code?

#   Number  |   Gender  |   GivenName   |   Surname |   StreetAddress   |   City        |   StateFull   |   ZipCode |   EmailAddress    |   Username    |   Password    |   TelephoneNumber |   MothersMaiden   |   Birthday    |   Age |   Occupation  |   Company |   Vehicle |   BloodType   |   Kilograms   |   Centimeters |   GUID    |   Latitude    |   Longitude
§   1   |   male    |   Estevan |   Rodrigues   |   Avenida 25 Abril 74 |   Viseu   |   Viseu   |   3510-150    |   [email protected]    |   Forenot |   EighahN5ei  |   2325827898  |   Correia |   1/10/1953   |   64  |   Home appliance repairer |   Maxaprofit  |   2005 Lancia Lybra   |   A+  |   71.3    |   169 |   d4f8d88d-afe1-4c63-821a-278883d6bb49    |   41.010337   |   -7.923389
§   2   |   male    |   Joao    |   Santos  |   R Afonso Albuquerque 27 |   Fonte Cova  |   Leiria  |   2425-609    |   [email protected]   |   Districe1986    |   ipe3ihae9oeKei  |   2445343980  |   Cardoso |   3/31/1986   |   31  |   School social worker    |   W. Bell & Co.   |   1998 Subaru SVX |   O+  |   98.9    |   185 |   021a1dc3-5b75-4868-bb03-333170ce9acb    |   39.980212   |   -8.911969

XML format:

<blooddonors>
     <donor>
          <Number>1</Number>
          <Gender>male</Gender>
          <GivenName>Estevan</GivenName>
          ...
      </donor>
     <donor>
          <Number>2</Number>
          <Gender>male</Gender>
          <GivenName>Joao</GivenName>
          ...
      </donor>
</blooddonors>
    
asked by anonymous 30.10.2017 / 16:51

1 answer

1

To convert from text to use class XmlTextWriter and if the % header header / strong> you have to use a bit of logic, a bit of and two layout to interact to solve this problem immediately, a minimum example :

XmlTextWriter writer = new XmlTextWriter(@"c:\temp\a\result.xml", null);
string[] lines = System.IO.File.ReadAllLines(@"c:\temp\a\texto.txt");

string header = lines.Where(x => x.Contains("#")).FirstOrDefault();
header = header.Substring(1);
string[] headers = header.Split('|').Select(x => x.Trim().TrimEnd().TrimStart()).ToArray();

string[] values = lines.Where(x => !x.Contains("#")).ToArray();

writer.WriteStartDocument();
writer.Formatting = Formatting.Indented;
writer.WriteStartElement("blooddonors");

for (int j = 0; j < values.Length; j++)
{
    string value = values[j];
    value = value.Substring(1);
    string[] v = value.Split('|').Select(x => x.Trim().TrimEnd().TrimStart()).ToArray();
    writer.WriteStartElement("donor");
    for (int i = 0; i < headers.Length; i++)
    {   
        writer.WriteElementString(headers[i], v[i]);
    }
    writer.WriteEndElement();
}            

writer.WriteFullEndElement();
writer.Close();
writer.Dispose();

In the output of this code you have the following : p>

<?xml version="1.0"?>
<blooddonors>
  <donor>
    <Number>1</Number>
    <Gender>male</Gender>
    <GivenName>Estevan</GivenName>
    <Surname>Rodrigues</Surname>
    <StreetAddress>Avenida 25 Abril 74</StreetAddress>
    <City>Viseu</City>
    <StateFull>Viseu</StateFull>
    <ZipCode>3510-150</ZipCode>
    <EmailAddress>[email protected]</EmailAddress>
    <Username>Forenot</Username>
    <Password>EighahN5ei</Password>
    <TelephoneNumber>2325827898</TelephoneNumber>
    <MothersMaiden>Correia</MothersMaiden>
    <Birthday>1/10/1953</Birthday>
    <Age>64</Age>
    <Occupation>Home appliance repairer</Occupation>
    <Company>Maxaprofit</Company>
    <Vehicle>2005 Lancia Lybra</Vehicle>
    <BloodType>A+</BloodType>
    <Kilograms>71.3</Kilograms>
    <Centimeters>169</Centimeters>
    <GUID>d4f8d88d-afe1-4c63-821a-278883d6bb49</GUID>
    <Latitude>41.010337</Latitude>
    <Longitude>-7.923389</Longitude>
  </donor>
  <donor>
    <Number>2</Number>
    <Gender>male</Gender>
    <GivenName>Joao</GivenName>
    <Surname>Santos</Surname>
    <StreetAddress>R Afonso Albuquerque 27</StreetAddress>
    <City>Fonte Cova</City>
    <StateFull>Leiria</StateFull>
    <ZipCode>2425-609</ZipCode>
    <EmailAddress>[email protected]</EmailAddress>
    <Username>Districe1986</Username>
    <Password>ipe3ihae9oeKei</Password>
    <TelephoneNumber>2445343980</TelephoneNumber>
    <MothersMaiden>Cardoso</MothersMaiden>
    <Birthday>3/31/1986</Birthday>
    <Age>31</Age>
    <Occupation>School social worker</Occupation>
    <Company>W. Bell &amp; Co.</Company>
    <Vehicle>1998 Subaru SVX</Vehicle>
    <BloodType>O+</BloodType>
    <Kilograms>98.9</Kilograms>
    <Centimeters>185</Centimeters>
    <GUID>021a1dc3-5b75-4868-bb03-333170ce9acb</GUID>
    <Latitude>39.980212</Latitude>
    <Longitude>-8.911969</Longitude>
  </donor>
</blooddonors>

30.10.2017 / 17:35