asp.net the method of saving data to XML and TXT file

Source: Internet
Author: User
Tags save file

XML is a data description language, the structure is relatively simple, not professionals can also read the language described in the content. XML represents the abbreviation for Extensible Markup Language (extensible Markup Language, meaning Extensible Markup Language). XML is a set of rules that define semantic markup that divides documents into many parts and identifies them. It is also a Meta markup language, defined as a syntactic language for defining other semantic, structured markup languages related to a particular domain.

Ways to save XML data:
In asp.net 2.0, the "XmlDocument" class is used to implement the operation of XML data to achieve the purpose described at the beginning of the article. The steps to implement the method are as follows:

<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.OleDb"%>

<script language= "VB" runat= "Server" >

Sub Page_Load (Sender as Object, E as EventArgs)

Dim strconnection as String
Dim strSQL as String
Dim Objdataset as New DataSet ()
Dim Objconnection as OleDbConnection
Dim Objadapter as OleDbDataAdapter

' Set the connection and query details
strconnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data source=" & Server.MapPath ("Northwind.mdb")
strSQL = "Select FirstName, LastName from Employees;"

' Open ' connection and set the command
Objconnection = New OleDbConnection (strconnection)
Objadapter = New OleDbDataAdapter (strSQL, objconnection)

' Fill the DataSet with the ' data
Objadapter.fill (Objdataset, "Employees")

Objdataset.writexml (Server.MapPath ("Employees.xml"))

Response.Write ("<a href= ' employees.xml ' >view xml file</a>")

End Sub

</script>


This article is an example of how to save DataGridView data to a txt document in asp.net

Using System.IO
public void SaveFile ()
{
Instantiate a Save File dialog box
SaveFileDialog SF = new SaveFileDialog ();
Set File Save type
sf. Filter = "TXT file |*.txt";
If the user does not enter an extension, the suffix is appended automatically
sf. AddExtension = true;
Set Title
sf. Title = "Write file";
If the user clicks the Save button
if (SF. ShowDialog () = = DialogResult.OK)
{
Instantiate a file stream---> associated with a write file
FileStream fs = new FileStream (SF. FileName, FileMode.Create);
Instantiate a streamwriter--> associated with FS
StreamWriter sw = new StreamWriter (FS);
Start writing
if (This.dataGridView1.Rows.Count < 1)
{
MessageBox.Show ("No data!") Export failed! "," hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
Else
{
for (int i = 0; i < this.datagridview1.rows.count-1 i)
{
Sw. WriteLine (This.datagridview1.rows[i]. Cells[0]. Value.tostring ());
}
Sw. Write (This.textBox1.Text);
Empty buffer
Sw. Flush ();
Close the stream
Sw. Close ();
Fs. Close ();
MessageBox.Show ("Save success!") "," hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}

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.