locked
Convert text file into XML file RRS feed

  • Question

  • User265788195 posted

    I need to convert a text file into XML file? How should start to do that? Any ideas please.

    BTW is ASCII file same as a text file?

    Friday, March 25, 2016 10:31 PM

Answers

  • User541108374 posted

    Hi,

    does the text file have some layout that indicates which can be transformed into specific xml elements? Of is it just a flat text file which you want to turn into an xml file with just one element? If so then read in the content of the file, create a new XmlElement and set its text to the text you retrieved from the text file you just read out. Then save it as an xml file.

    For reading out the file in either at once or line by line:

    // The files used in this example are created in the topic
            // How to: Write to a Text File. You can change the path and
            // file name to substitute text files of your own.
    
            // Example #1
            // Read the file as one string.
            string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");
    
            // Display the file contents to the console. Variable text is a string.
            System.Console.WriteLine("Contents of WriteText.txt = {0}", text);
    
            // Example #2
            // Read each line of the file into a string array. Each element
            // of the array is one line of the file.
            string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");
    
            // Display the file contents by using a foreach loop.
            System.Console.WriteLine("Contents of WriteLines2.txt = ");
            foreach (string line in lines)
            {
                // Use a tab to indent each line of the file.
                Console.WriteLine("\t" + line);
            }

    Taken from https://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx. Note that the Console.... won't work as you're busy with ASP.NET so focus only on the file reading part.

    For writing out to xml: 

    string xml = ""; // the string you just obtained from the text file
    
    File.WriteAllText("foo.xml", xml);

    Taken from http://stackoverflow.com/questions/4721735/how-to-save-this-string-into-xml-file

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 26, 2016 2:57 PM

All replies

  • User409696431 posted

    What would you be doing in this conversion?    An XML file is a text file, it just contains XML syntax.   If you are talking about a text file that has no XML syntax, how would you know what the XML syntax should be?

    Friday, March 25, 2016 10:55 PM
  • User541108374 posted

    Hi,

    does the text file have some layout that indicates which can be transformed into specific xml elements? Of is it just a flat text file which you want to turn into an xml file with just one element? If so then read in the content of the file, create a new XmlElement and set its text to the text you retrieved from the text file you just read out. Then save it as an xml file.

    For reading out the file in either at once or line by line:

    // The files used in this example are created in the topic
            // How to: Write to a Text File. You can change the path and
            // file name to substitute text files of your own.
    
            // Example #1
            // Read the file as one string.
            string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");
    
            // Display the file contents to the console. Variable text is a string.
            System.Console.WriteLine("Contents of WriteText.txt = {0}", text);
    
            // Example #2
            // Read each line of the file into a string array. Each element
            // of the array is one line of the file.
            string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");
    
            // Display the file contents by using a foreach loop.
            System.Console.WriteLine("Contents of WriteLines2.txt = ");
            foreach (string line in lines)
            {
                // Use a tab to indent each line of the file.
                Console.WriteLine("\t" + line);
            }

    Taken from https://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx. Note that the Console.... won't work as you're busy with ASP.NET so focus only on the file reading part.

    For writing out to xml: 

    string xml = ""; // the string you just obtained from the text file
    
    File.WriteAllText("foo.xml", xml);

    Taken from http://stackoverflow.com/questions/4721735/how-to-save-this-string-into-xml-file

    Grz, Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 26, 2016 2:57 PM
  • User36583972 posted

    Hi nissan,

    Text file format determines how you converted to xml file. The following simple example for your reference.

    Assuming you need to add an XML element for every line in text, you can write similar to the following (XLINQ).

    String[] data = File.ReadAllLines("TextFile.txt");
    XElement root = new XElement("root",
                                from item in data
                                select new XElement("Line",item));
    root.Save("XmlFile.Xml");

    The another format text file converts to xml for your reference.

    Convert flat text file to XML in C#:

    http://stackoverflow.com/questions/15402493/convert-flat-text-file-to-xml-in-c-sharp

    Best Regards,

    Yohann Lu

    Monday, March 28, 2016 4:33 AM