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 ); } } } |