ASP.NET - Converting a Text File into an XML File

Asked By brian
08-Mar-10 07:36 AM
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
  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

  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.
  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.
  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,
http://www.sql-server-helper.com/tips/comma-delimited-output.aspx
Create New Account
help
Any one send frequently asked Important questions in C# .Net, ADO .Net, Asp .Net and Sql Server. . . . . . . . tx in Advance. . . . . . Hi, Find this. . (B)What is an IL? (B)What is a objects in Remoting? (A) What are the ways in which client can create object on server in CAO model? (A) Are CAO stateful in nature? (A) To create objects in CAO page ? (I) Can we post and access view state in another application? (I) What is SQL Cache Dependency in ASP.NET 2.0? (I) How do we enable SQL Cache Dependency in ASP.NET 2.0? (I) What is Post Cache substitution? (I) Why Config”? (B) What is a SESSION and APPLICATION object? (A) What is the difference between ‘Server.Transfer’ and ‘response. Redirect’ ? (A)What is the difference between Authentication and authorization? (I) what proper? (A) If client side validation is enabled in your Web page, does that mean server side code is not run. (A)Which JavaScript file is referenced for validating the validators A)What is the use of <%@ page aspcompat = true %> attribute? B) Explain the differences between Server-side and Client-side code? (I)Can you explain Forms authentication in detail? (A)How
of sqlserver to ms access database Hello sir, i have one table as purchaseOrder in sql server i am using datagrid view the table sqlconection con = new sqlconnection(" "); SqlCeCommand com = new SqlCeCommand("select * from purchaseOrder", con); SqlCeDataAdapter da = new SqlCeDataAdapter(); da.Fill(ds); DataTable dt = ds.Tables[1]; dataGridView1.DataSource = ds.Tables[1]; i want same table same data database i create one database as pra.mdb i want move purchaseOrder table of my sql sever to purchaseOrder table of ms access please say any solutions HI I have taken i have inserted data into database. try this code- string stConnection1 = "constring1"; string stConnection2 = "constring2"; OleDBConnection cn1 = new OleDBConnection (stConnection1 ); / / access OleDBCommand cmd1 = new OleDBCommand ("select * from employees", cn1); OleDBDataAdapter da1 = new OleDBDataAdapter (cmd1); OleCommandBuilder cb = new OleCommandBuilder(da1); SqlConnection
data • In-memory cache of a database table • Used to manipulate a row in a DataTable • Used to define the columns in a DataTable • Used to relate 2 DataTable s to each other • Used to create views on DataSets System.Data DataTable DataRow DataRelation DataColumn DataViewManager DataSet DataSet Tables DataTable Putting the Objects Together… Relations DataRelation DataRelation DataRow(s) DataColumn Constraint(s) DataTable DataTable DataView DataViewManager Working Data - The DataSet • An in-memory cache of data from a data are used to add & remove tables & relations • Properties of Interest: • Tables : Returns the collection of DataTable objects • Relations : Returns the collection of DataRelation s • Namespace : Gets or sets the namespace of
upload their Excel file and want to parse that file data and save in to sql server data base . . . . . . .please help me how cani achieve the above in simpler way. please give let me know its working for me perfectly using System.Data; using System.Data.OleDb; OleDbConnection con = new OleDbConnection(@ "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = C: \ Book1.xls;Extended Properties = Excel 8.0" ); OleDbDataAdapter da = new OleDbDataAdapter( "select * from MyObject" , con); DataTable dt = new DataTable(); da.Fill(dt); / * Read the DataTable Item and Insert it One by One into Database
Transfering data from Excel to Sql Hello friends thanks for ur support. I used the code given by u, and the it withouyt using the bulkcopy method and while entering the data from Excel sheet to Sql database it should check the column ID which is primary key in sql database if the records already exists then that records will not be inserted , and if is the code for you string xConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = " + Server.MapPath( "ExcelImport.xls" ) + ";" + "Extended Properties = Excel 8.0;" ; using (OleDbConnection connection = new OleDbConnection(xConnStr)) { OleDbCommand command = new OleDbCommand( "Select * FROM [Sheet1$]" , connection); connection.Open(); / / Create DbDataReader to Data Worksheet using (DbDataReader dr = command