How to: Use Visual C # .NET to save the ADO.NET data set as XML (from MSDN)

xiaoxiao2021-03-06  78

The release number of this article has been CHS309183

For Microsoft Visual Basic .NET versions of this article, see

308064.

For Microsoft Visual C .NET versions of this article, see

309184.

This article references the following Microsoft .NET Framework Class Bank Name Space:

System.Data.Oledb System.io

This task content

summary

Require technical description to create projects and add code Remarks

Summary This article demonstrates how to put ado.net

The DataSet object is stored as Scalable Markup Language (XML).

Back to top

Require the following list lists the recommended hardware, software, network structure, and service pack required:

Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server Microsoft Visual Studio .NET This article assumes that you are familiar with the following topics:

Visual Studio .NET ADO.NET Foundation and Syntax XML Foundation

Back to top

Technical note you can use

Writexml method write

The XML architecture and data of the DataSet object. XML data can write files,

Stream class,

XMLWRITER class, or writing

TextWriter class. Can be used according to your needs

One of the two sets of overloading methods of WriteXML. The first set of four overload methods have only one parameter, and the second set of four overload methods has a parameter except the above parameters (

XMLWRITEMODE). This section describes each of these methods.

To be

DataSet's current architecture and data writes the specified file, use the following code:

Void DataSet.writexml (String FileName) To write

DataSet's current architecture and data, please use the specified

System.io.textwriter class.

The purpose of the TextWriter class is to perform character output.

Void Dataset.writexml (System.io.TextWriter Writer) To write

DataSet's current architecture and data, please use the specified

System.io.stream.

The Stream class is designed for byte input and output.

Void dataset.writexml (System.io.Stream Stream)

DataSet's current architecture and data writing specified

System.xml.xmlWriter, use the following code. This provides a fast, cached and only a method that generates a data stream or file containing XML data, so that it conforms to the World Wide Web Federation (W3C) XML 1.0 specification and the namespace in the XML specification.

Void Dataset.writeXml (Sytem.xml.xmlwriter Writer)

System.Data.xmlwritemode enumeration specifies how to write

DataSet XML data and architecture.

XMLWRITEMODE contains the following options:

DiffGram: Write the entire DataSet as a DiffGram. IgnoresChema: Write the current content of DataSet as XML data, does not require the "XML Schema Definition Language" (XSD) architecture. Writeschema: Write the current content of the DataSet as XML data, use the associated structure as an inline XSD architecture.

Back to top

Creating a project and adding code The following example creates one from the customer table in the Rosswind database

DataSet, and use

Writexml method

DataSet remains XML. This example demonstrates how two common overload versions of WriteXML. For additional examples, see each overload topic on this method on the MSDN.

Start Microsoft Visual Studio .NET. Create a Windows Application project in Visual C # .NET. Make sure your project contains a reference to System.Data namespace, if not included, add a reference to this namespace. Place the two Button controls on Form1. Change the Name property of the first button to btnWriter and change the Text property to Writer. Change the Name property of the second button to btnfile and change the text attribute to File. Using the USING statement in System and System.Data namespace, this is not required to limit the declarations in these namespaces in the code. Using system;

Using system.data;

Using system.data.sqlclclient; copy the following code and paste the event handler to two buttons: private void btnWriter_Click (Object Sender, System.EventArgs E) {

String myconnectionstring = "User ID = login; password = password; initial catalog = northwind;

Data Source = ServerName ";

String myselectquery = "select * from customers";

System.IO.FileStream MyFileStream = New System.IO.FileStream

("C: //myschema.xml", system.io.filemode.create;

System.xml.xmlTextWriter myXmlTextWriter = new system.xml.xmlTextWriter

(MyFileStream, System.Text.Encoding.Unicode);

Try {

SqlConnection Con = New SqlConnection (MyConnectionstring);

SqlDataAdapter Dacust = New SqlDataAdapter (MySelectQuery, Con);

DataSet DS = New Dataset ();

Dacust.Fill (DS, "Cust");

// Write the xml along with the inline schema (default).

DS.WriteXml (MyXMLTextWriter, XMLWRITE.WRITESCHEMA);

// Write the XML ONLY.

//ds.writexml (MyXMLTextWriter, XMLWRITE.IGNORESCHEMA);

// Write the XML AS A DIFFGRAM.

//ds.writexml (MyXMLTextWriter, XMLWRITEMODE.DIFFGRAM);

MessageBox.show ("Save Complete");

}

Catch (system.exception ex) {

Messagebox.show (ex.totring ());

}

Finally {

MyXmlTextWriter.Close ();

MyFileStream.Close ();

}

}

Private void btnfile_click (object sender, system.eventargs e) {string myconnectionstring = "user id = login; password = password; initial catalog = northwind;

Data Source = ServerName ";

String myselectquery = "select * from customers";

Try {

SqlConnection Con = New SqlConnection (MyConnectionstring);

SqlDataAdapter Dacust = New SqlDataAdapter (MySelectQuery, Con);

DataSet DS = New Dataset ();

Dacust.Fill (DS, "Cust");

// Write the xml along with the inline schema (default).

DS.WRITEXML ("C: //Myschema.xml", XMLWRITE.WRITESCHEMA);

// Write the XML ONLY.

//ds.writexml("c://myschema.xml ", XMLWRITE.IGNORESCHEMA);

// Write the XML AS A DIFFGRAM.

//ds.writexml ("c://myschema.xml ", XMLWRITE.DIFFGRAM);

MessageBox.show ("Save Complete");

}

Catch (system.exception ex) {

Messagebox.show (ex.totring ());

}

} Modify the contact string (MyConnectionstring) and XML file path according to your environment. Save the project. On the Debug menu, click Start to run your project. Click any button to store the data to the file. Open your browser and open the XML file. Note that data in the DataSet has successfully stayed as the XML format based on the XMLWRItemode you specify.

Back to top

Note

You can use the WriteXmlschema method if you only write to the XML architecture. You can use the getXML method if you only need to get the XML representation of data in the DataSet.

Back to top

Refer to additional information, click the following article number to see the article in the Microsoft Knowledge Base:

262450 HOWTO: A C Sample of Ado RecordSet XML Persistence (How to: an ADO Recordset XML Persistence C Example)

311566 How to: Use Visual C # .NET to read XML data into data sets For more information about ADO.NET objects and syntax, see the following topics in the Microsoft .NET Framework Software Development Kit (SDK) document:

Use ADO.NET access data http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaccessingDatawithadonet.asp

Back to top

The information in this article applies to:

Microsoft ADO.NET (provided with .NET Framework) Microsoft Visual C # .NET (2002) Received: 2002-6-17 (1.0) Keyword KBDsupport KBGRPDSMDAC KBGRPDSVBDB KBHOWTO KBHOWTOMASTER KBIO KBOLEDB KBSYSTEMDATA KB309183

转载请注明原文地址:https://www.9cbs.com/read-109027.html

New Post(0)