Converting a Text File into an XML File

Asked By brian
08-Mar-10 07:36 AM
Earn up to 0 extra points for answering this tough question.
Hi,

I have a text file with data and i need to convert the file into an xml file so that i can easily read the values.How do i do this in asp.net
Pls Help
Thanks

  re: Converting a Text File into an XML File

Sagar P replied to brian
08-Mar-10 07:42 AM

The function "Text2XML" itself works by creating an XML object template using the fields defined in the first line of the text field. This template is then repeatedly copied (or cloned), populated with a new line worth of data, and added to the collection node. You could also create the element manually by buffering the XML structure as a string, but by taking this route you lose the advantage of possible error checking. (Note: I haven't implemented an error handling mechanism here, but if it were critical, you would use the XML DOM parseError object to output to a log or perform other error handling). Thus, to generate the XML file for the sample user list, you'd make the function call:

Dim xmlDoc
Set xmlDoc=Text2XML("employees.txt","employee","employees",",")

Check out this link to know more abt it;
http://www.devx.com/getHelpOn/10MinuteSolution/20356

Also check this;
http://www.vclcomponents.com/s/0__/convert_a_text_file_into_xml_file_using

  re: Converting a Text File into an XML File

Peter Bromberg replied to brian
08-Mar-10 07:44 AM
ASP.NET has nothing to do with converting a text file into an XML document. You do that with your preferred .NET language, e.g. C# or VB.NET.

Assuming the text file is properly delimited, e.g. CSV or similar format, there are several different approaches. The easiest one for a beginner is to load the file into a string, then split the string on the newline character ("\r\n" ) so you get rows, then split each row using the comma into a string array of items.

Then you would simply create a new string and iterate over the rows and items, building the correct XML beginning and ending  tags and sticking each item in between them.

If you search around a bit I bet you could find some class library that somebody has written that already does this, and you will not have to reinvent the wheel.

  re: Converting a Text File into an XML File

Adam Houldsworth replied to brian
08-Mar-10 07:46 AM
Hi,

This isn't really specific to ASP.NET, just so you are aware.  Is your text file in any particular structure, CSV, tab delimited, line delimited?

XML is a structured data format, and its structure is entirely described by you.  What input have you got and what are you trying to achieve with it?

Its a bit of a general question at the moment.
  re: Converting a Text File into an XML File
F Cali replied to brian
08-Mar-10 07:58 AM
Another way of reading a text file and saving it to an Xml is to load the text file using OleDbCommand/OleDbConnection into a DataTable then use the WriteXml method of the DataTable:

OleDbConnection oleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\YourFolder\;Extended
Properties=""text;HDR=no;FMT=Delimited""")
OleDbCommand oleDbCommand = new ("SELECT * FROM [YourTextFile.txt]", oleDbConnection);
DataTable dataTable = new DataTable("YourData");
OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(oleDbCommand);

oleDbDataAdapter.Fill(dataTable);
dataTable.WriteXml("YourXmlFile.xml");

Regards,
SQL Server Helper
Create New Account