Convert TXT to XML using C#

Asked

Viewed 666 times

-3

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

#   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>
  • I didn’t get an answer on the other question so I tried again, thank you

  • And then that code helps you?

  • Yes friend, enough! Thanks man!

1 answer

1


To transform from text to use the class Xmltextwriter and whether the layout be that with a header and the rest the values of each item of the header have to use a little logic, a little bit of and two for to interact to solve this problem immediately, a minimal 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 :

<?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>

References:

  • Takes negative votes without necessity disturbs the progress of the site, a code well done and well explained with references and we take votes without deserving.

  • Remember that I did not find on the site an answer that could be compatible with this question that could give a way, so my concern to put a related answer.

  • And if the answer has any problems you can let me know!

  • 1

    I gave a positive vote, the staff just wants to receive, but does not think to help others not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.