Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating an XML 1

Status
Not open for further replies.

ac11nyc

Programmer
Oct 1, 2003
94
US
Does anyone know how i can create an XML file from data coming back from a database? If you have any examples - that would be great
 
If you retrieve your data as a DataSet this has a built in WriteXml method which at its simplest takes a filename as a string and writes the DataSet to that file as XML. Heres the example from the framework docs, this complicates the issue slightly by creating a Stream to access the file and uses an XmlTextWriter object to write the XML to the file.
Code:
private void WriteXmlToFile(DataSet thisDataSet) {
    if (thisDataSet == null) { return; }
    // Create a file name to write to.
    string filename = "myXmlDoc.xml";
    // Create the FileStream to write with.
    System.IO.FileStream myFileStream = new System.IO.FileStream
       (filename, System.IO.FileMode.Create);
    // Create an XmlTextWriter with the fileStream.
    System.Xml.XmlTextWriter myXmlWriter = 
       new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
    // Write to the file with the WriteXml method.
    thisDataSet.WriteXml(myXmlWriter);   
    myXmlWriter.Close();
 }

Here's a link to the online documentation for this method which will explain it better than me.


Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Easiest way is to create a dataset and use the Dataset's WriteXML to create the Xml file
You have to import System.Xml and System.XML.Xsl and asp.net should have the proper permission to write the file to where ever you decide to store it

Code:
Imports System.Xml
Imports System.Xml.Xsl

Code:
[highlight]Public variable in your class [/Highlight]
Public appPath As String = context.Request.ApplicationPath
    Public strFile As String = appPath & "/XMLFolder\MyTable.xml"

Code:
[highlight]put this code in page load[/highlight]
if If Not IsPostBack Then
Dim myConnection As SqlConnection = New SqlConnection("MySQLServer")
Dim Myds as DataSet = new DataSet()
dim sSql as string = "SELECT * FROM MyTable"

'Declare an Adapter and Fill Dataset
dim adapter as SqlDataAdapter = new SqlDataAdapter(sSql, myConnection)
adapter.Fill(Myds)
myConnection.Close() 
Myds.WriteXml(StrFile, XmlWriteMode.IgnoreSchema)
End if
If you run this code a file should be created and saved to the directory of your choice
let me know if you need more help
Good luck


Note:Copying and Pasting is NoT Creativity.
What would you attempt to accomplish if you knew you would not fail?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top