using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace txt_xml { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnOpen_Click(object sender, EventArgs e) { openFileDialog1.Filter = "Text Files|*.txt|All Files|*.*"; openFileDialog1.ShowDialog(); if (openFileDialog1.FileName != "") { this.Text = openFileDialog1.FileName; // Output file is the inputfile, but extension xml instead of txt String outputFile = openFileDialog1.FileName.Replace(".txt", ".xml"); // If the file alrady exists, ask if they want to replace it. if (File.Exists(outputFile)) { DialogResult result; result = MessageBox.Show(outputFile+ " already exists. Do you want to replace it?", "Confirm Save As",MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); // If they say no, signal end by changing filename to null string. if (result == DialogResult.Yes) outputFile = ""; } // If thye haven't ended, write the output file. if (outputFile != "") { Text = "Writing to " + outputFile; // Get all lines from the input file. string[] input = System.IO.File.ReadAllLines(openFileDialog1.FileName); // Open the output file to write using (System.IO.StreamWriter file = new System.IO.StreamWriter(outputFile)) { // Write header line: file.WriteLine(""); file.WriteLine(""); foreach (string line in input) { // Example: AL Alabama becomes Alabama int space = line.IndexOf(" "); String key = line.Substring(0, space); String data = line.Substring(space + 1); file.WriteLine("<" + key + ">" + data + ""); } // Add the end tag and close the file. file.WriteLine(""); file.Close(); } } } } } }