C # convert TXT text to XML text

Source: Internet
Author: User

Currently, many software uses XML files as the data source, while many data tools such as Pb can only be saved as txt, Excel, and other formats. Therefore, a tool is required to convert TXT text into XML files. Google once, did not find the appropriate, impulsive use C # To write a TXT text to the XML format text of the small program, the Code is as follows.

Create a Windows application named frmtxtxml and add the following content to the form:

One button "toolstripopen" and text is "Open File ";

One button "toolstripconvert" and text is "Conversion ";

One button "toolstripsaveas" and text is "Save ";

A text box "txttxt" that displays the TXT text;

A text box "txtxml5" that displays text in XML format;

Public
Partial class frmtxtxml: Form

{

String txtcontent =
String. empty;

//

Public frmtxtxml ()

{

Initializecomponent ();

}

 

// Open the TXT file

Private
Void toolstripopen_click (Object sender,
Eventargs E)

{

Using (openfiledialog filedialog = new
Openfiledialog ())

{

Filedialog. Filter = "text file (*. txt) | *. txt ";

If (filedialog. showdialog () = dialogresult. OK)

{

String filename = filedialog. filename;

If (! String. isnullorempty (filename ))

{

Using (streamreader ST = new
Streamreader (filename, system. Text. encoding. getencoding ("GBK ")))

{

Txtcontent = ST. readtoend ();

// Read the TXT file to the txttxt text box

This.txt TXT. Text = txtcontent;

St. Close ();

}

}

}

}

}

 

// Convert the TXT file content into XML format

Private
Void toolstripconvert_click (Object sender, eventargs E)

{

Try

{

// Break down the TXT content into a row Array

String [] lines =
This.txt TXT. Text. Split (New
String [] {"/R/N "},
Stringsplitoptions. None );

String [] Heads =
NULL;

If (lines! =
Null & lines. length> 0)

{

// Read the first row of data, which is the node description data of the XML file

Heads = lines [0]. Split (newstring [] {
"/T"}, stringsplitoptions. None );

// MessageBox. Show (heads. length. tostring () + "" + heads [0]);

}

//

Stringbuilder sb = new
Stringbuilder ();

SB. append ("<? XML version =/"1.0/" encoding =/"GBK/"?> "). Append (environment. newline). append (" <dataroot> "). append (environment. newline );

// Generate an XML Node

For (INT I = 1; I <lines. length; I ++)

{

If (lines [I] = NULL | lines [I]. Trim (). Length <1)

Continue;

String [] info = lines [I]. Split (newstring [] {
"/T"}, stringsplitoptions. None );

SB. append (createnode (heads, Info ));

}

SB. append ("</dataroot> ");

This.txt XML. Text = sb. tostring ();

}

Catch (exception exp)

{

MessageBox. Show (exp. Message );

}

}

 

// Generate an XML Node

Private
String createnode (string [] Head,
String [] info)

{

Stringbuilder sb =
New stringbuilder ();

SB. append ("<record>"). append (environment. newline );

For (INT I = 0; I

{

SB. append ("<" + head [I] + ">" + info [I] +
"</" + Head [I] + ">"). append (environment. newline );

}

SB. append ("</record>"). append (environment. newline );

Return sb. tostring ();

}

 

// Save the TXT text box content as an XML file

Private
Void toolstripsaveas_click (Object sender,
Eventargs E)

{

Try

{

String filename = "";

Using (savefiledialog filedialog = new
Savefiledialog ())

{

Filedialog. Filter =
"XML data file (*. XML) | *. xml ";

If (filedialog. showdialog () = dialogresult. OK)

{

Filename = filedialog. filename;

If (! String. isnullorempty (filename ))

{

Filestream FS = new
Filestream (filename, filemode. Create );

// Obtain the byte array

Byte [] DATA = system. Text. encoding. getencoding ("GBK" ).getbytes(this.txt XML. Text );

// Start writing

FS. Write (data, 0, Data. Length );

// Clear the buffer and close the stream

FS. Flush ();

FS. Close ();

}

}

}

MessageBox. Show (string. Format ("the file is successfully saved to {0}", filename ));

}

Catch (exception exp)

{

MessageBox. Show (exp. Message );

}

}

}

 

Run the program to convert the TXT text to the following XML content.

Emp_no emp_name emp_xb emp_bir

11865 Zhang San 1 00:00:00

10 sanli 0 00:00:00

100 Zhang Si Li 0 00:00:00

1000 Zhang Wuli 1 00:00:00

100001 sheets 6 Li 0 00:00:00

100002 Zhang Qi Li 1 00:00:00

<? XML version = "1.0" encoding = "GBK"?>

<Dataroot>

<Record>

<Emp_no> 11865 </emp_no>

<Emp_name> Zhang San </emp_name>

<Emp_xb> 1 </emp_xb>

<Emp_bir> 00:00:00 </emp_bir>

</Record>

<Record>

<Emp_no> 10 </emp_no>

<Emp_name> Zhang sanli </emp_name>

<Emp_xb> 0 </emp_xb>

<Emp_bir> 00:00:00 </emp_bir>

</Record>

<Record>

<Emp_no> 100 </emp_no>

<Emp_name> Zhang Sili </emp_name>

<Emp_xb> 0 </emp_xb>

<Emp_bir> 00:00:00 </emp_bir>

</Record>

<Record>

<Emp_no> 1000 </emp_no>

<Emp_name> Zhang Wuli </emp_name>

<Emp_xb> 1 </emp_xb>

<Emp_bir> 00:00:00 </emp_bir>

</Record>

<Record>

<Emp_no> 100001 </emp_no>

<Emp_name> Zhang liuli </emp_name>

<Emp_xb> 0 </emp_xb>

<Emp_bir> 00:00:00 </emp_bir>

</Record>

<Record>

<Emp_no> 100002 </emp_no>

<Emp_name> Zhang Qili </emp_name>

<Emp_xb> 1 </emp_xb>

<Emp_bir> 00:00:00 </emp_bir>

</Record>

</Dataroot>

 

Original article: http://blog.csdn.net/chuangxin/article/details/5336756

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.