A method is created to tokenize the data in the file into individual data tokens. Each call to this function returns one of the data elements. The method takes in a StreamReader.
The code iterates through until there are no data tokens remaining; inserting each token into an XML document. This is done in the Main function, which takes two command line arguments. The first argument is the file to read from, the second is the file to write to. The comments within the code provide a description of the actions taking place.
[C#]
using System;
using System.IO;
using System.Xml;
namespace TextFiletoXml
{
class TextFiletoXml
{
public static String file;
public static StreamReader stream;
public static XmlTextWriter xwriter;
public static String val;
public static String NextToken(StreamReader stream)
{
int temp = stream.Read();
String t1 = "";
while(temp != -1 && temp != (int)'|')
{
t1 += (char)temp;
temp = stream.Read();
}
return t1;
}
public static void Main(string[] args)
{
file = args[0];
//Create a new stream representing the file we are
//reading from.
stream = new StreamReader(file, true);
//Create a new XmlTextWriter.
xwriter = new XmlTextWriter(args[1],System.Text.Encoding.UTF8);
//Write the beginning of the document including the
//document declaration. Standalone is true.
xwriter.WriteStartDocument(true);
//Write the beginning of the "data" element. This is
//the opening tag to our data
// Get the first data element from the file.
val = NextToken(stream);
//create a new element with each data token from //the stream.
while(val != "")
{
xwriter.WriteElementString("item","www.alpineskihouse.com", val);
val = NextToken(stream);
}
//End the "data" element.
xwriter.WriteEndElement();
//End the document
xwriter.WriteEndDocument();
//Flush the xml document to the underlying stream and
//close the underlying stream. The data will not be
//written out to the stream until either the Flush()
//method is called or the Close() method is called.
xwriter.Close();
}
}
}
A method is created to tokenize the data in the file into individual data tokens. Each call to this function returns one of the data elements. The method takes in a StreamReader.
The code iterates through until there are no data tokens remaining; inserting each token into an XML document. This is done in the Main function, which takes two command line arguments. The first argument is the file to read from, the second is the file to write to. The comments within the code provide a description of the actions taking place.
[C#]
using System;
using System.IO;
using System.Xml;
namespace TextFiletoXml
{
class TextFiletoXml
{
public static String file;
public static StreamReader stream;
public static XmlTextWriter xwriter;
public static String val;
public static String NextToken(StreamReader stream)
{
int temp = stream.Read();
String t1 = "";
while(temp != -1 && temp != (int)'|')
{
t1 += (char)temp;
temp = stream.Read();
}
return t1;
}
public static void Main(string[] args)
{
file = args[0];
//Create a new stream representing the file we are
//reading from.
stream = new StreamReader(file, true);
//Create a new XmlTextWriter.
xwriter = new XmlTextWriter(args[1],System.Text.Encoding.UTF8);
//Write the beginning of the document including the
//document declaration. Standalone is true.
xwriter.WriteStartDocument(true);
//Write the beginning of the "data" element. This is
//the opening tag to our data
// Get the first data element from the file.
val = NextToken(stream);
//create a new element with each data token from //the stream.
while(val != "")
{
xwriter.WriteElementString("item","www.alpineskihouse.com", val);
val = NextToken(stream);
}
//End the "data" element.
xwriter.WriteEndElement();
//End the document
xwriter.WriteEndDocument();
//Flush the xml document to the underlying stream and
//close the underlying stream. The data will not be
//written out to the stream until either the Flush()
//method is called or the Close() method is called.
xwriter.Close();
}
}
}