How to save a dataset as an XML file

Source: Internet
Author: User
Tags root directory visual studio
Xml XML and. NET are completely integrated, many. NET server controls have one or more capabilities to create XML files, and the DataSet class contains several methods of creating XML files, in which we will create an example of a connection between the dataset and the XML, and the role of this example is to read from the database The data is filled in the DataSet object and then saved from the DataSet object to the XML file to disk, and, of course, we can create a schema file associated with it for the XML file.
Saving the dataset as an ASP.net paging file (. aspx) in an XML file is simple and even laughable, essentially, it doesn't contain a critical code, all we need to do is add a message--"done", The code that really embodies the functionality is in the Code-behind code file, and the following is the ASP.net Web page (. aspx):
<%@ Page language= "vb" src= "DataSetToXML.aspx.vb" inherits= "Datasettoxml"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<title>DataSetToXML</title>
<meta name= "generator" content= Microsoft Visual Studio.NET 7.0 >
<meta name= "Code_language" content= "Visual Basic 7.0" >
<meta name=vs_defaultclientscript content= "JavaScript" >
<meta name=vs_targetschema content= "http://schemas.microsoft.com/intellisense/ie5" >
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
</form>
</body>
In fact, the Code-behind file is not very complex, most of its code is familiar to us, such as using the DataAdapter object to populate the DataSet object, in order to make the XML will not become very large, We add top 10 to the SQL SELECT statement that queries data from the Customers table of the Northwind data, with only two lines for the really useful code, one for outputting the dataset as an XML file and the other for creating its schema file, In the article I marked it red, in this example we used two methods of the DataSet class: WriteXml and WriteXmlSchema, Server.MapPath is used to write two files to the root directory in the Web application, the two files will be called "customers.xml" and "custmers.xsd", the code is as follows:
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class datasettoxml:inherits System.Web.UI.Page

Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
Dim objconn as SqlConnection
Dim strSQL as String

strSQL = "SELECT top * from Customers"
objconn = New SqlConnection (configurationsettings.appsettings ("ConnectionString"))

Dim Sdacust as New SqlDataAdapter (strSQL, objconn)
Dim Dstcust as New DataSet ()

Sdacust.fill (Dstcust, "Customers")
' Save data to XML file and schema file '
Dstcust.writexml (Server.MapPath ("Customers.xml"), XmlWriteMode.IgnoreSchema)
Dstcust.writexmlschema (Server.MapPath ("customers.xsd"))
End Sub

End Class
I am sorry to have written such a simple article, but in fact, the real apology should be Microsoft's. NET, it is so easy for us to convert data from datasheet to XML file, I hope you can believe it!



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.