create XML file in vb.net

You don't have to create xml file explicitly if you use say XMLTextWriter but rather it will Create file, or overwrite if exists i.e.

VB.NET:
Dim objXMLTW as new XMLTextWriter(Application.StartUpPath & "/myXML.xml")
'now you can continue with writing elements

HTH
Regards ;)

edit: also you can use file class for the purpose i.e. File.Create(Application.StartUpPath & "/myXML.xml")
 
For .NET 2.0 Microsoft recommends using the XMLWriter.Create method:

VB.NET:
[COLOR=black][FONT=Courier New][COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] createXMLFile()
    [COLOR=blue]Dim[/COLOR] settings [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] XmlWriterSettings()
    settings.Indent = [COLOR=blue]True[/COLOR]
    settings.NewLineOnAttributes = [COLOR=blue]True[/COLOR]
    [COLOR=blue]Dim[/COLOR] writer [COLOR=blue]As[/COLOR] XmlWriter = XmlWriter.Create([COLOR=maroon]"test.xml"[/COLOR], settings)
    writer.WriteStartDocument([COLOR=blue]True[/COLOR])
    writer.WriteComment([COLOR=maroon]"My first XML"[/COLOR])
    writer.WriteStartElement([COLOR=maroon]"Don_Delegate_Info"[/COLOR])
    writer.WriteStartElement([COLOR=maroon]"Name"[/COLOR])
    writer.WriteString([COLOR=maroon]"Don Delegate"[/COLOR])
    writer.WriteEndElement()
    writer.WriteStartElement([COLOR=maroon]"Country"[/COLOR])
    writer.WriteString([COLOR=maroon]"Belgium"[/COLOR])
    writer.WriteEndElement()
    writer.WriteEndElement()
    writer.WriteEndDocument()
    writer.Close()
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
[/FONT][/COLOR]

will write:
VB.NET:
[COLOR=black][FONT=Courier New][COLOR=blue][COLOR=black][FONT=Courier New][COLOR=blue]<?[/COLOR][COLOR=maroon]xml [/COLOR][COLOR=red]version[/COLOR][COLOR=blue]=[/COLOR]"[COLOR=blue]1.0[/COLOR]" [COLOR=red]encoding[/COLOR][COLOR=blue]=[/COLOR]"[COLOR=blue]utf-8[/COLOR]" [COLOR=red]standalone[/COLOR][COLOR=blue]=[/COLOR]"[COLOR=blue]yes[/COLOR]"[COLOR=blue]?>[/COLOR]
[COLOR=blue]<!--[/COLOR][COLOR=green]My first XML[/COLOR][COLOR=blue]-->[/COLOR]
[COLOR=blue]<[/COLOR][COLOR=maroon]Don_Delegate_Info[/COLOR][COLOR=blue]>[/COLOR]
[COLOR=blue]<[/COLOR][COLOR=maroon]Name[/COLOR][COLOR=blue]>[/COLOR]Don Delegate[COLOR=blue]</[/COLOR][COLOR=maroon]Name[/COLOR][COLOR=blue]>[/COLOR]
[COLOR=blue]<[/COLOR][COLOR=maroon]Country[/COLOR][COLOR=blue]>[/COLOR]Belgium[COLOR=blue]</[/COLOR][COLOR=maroon]Country[/COLOR][COLOR=blue]>[/COLOR]
[COLOR=blue]</[/COLOR][COLOR=maroon]Don_Delegate_Info[/COLOR][COLOR=blue]>[/COLOR]
[/FONT][/COLOR]
[/COLOR][/FONT][/COLOR]
 
Last edited:
In .net 2.0 you can also write XML from a data table, this code comes from the local help and the string at the end from Kulrom :

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] table [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataTable = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataTable([/SIZE][SIZE=2][COLOR=#800000]"ParentTable"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]' Declare variables for DataColumn and DataRow objects.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] column [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataColumn
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] row [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow
[/SIZE][SIZE=2][COLOR=#008000]' Create new DataColumn, set DataType, ColumnName 
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' and add to DataTable. 
[/COLOR][/SIZE][SIZE=2]column = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn()
column.DataType = System.Type.GetType([/SIZE][SIZE=2][COLOR=#800000]"System.Int32"[/COLOR][/SIZE][SIZE=2])
column.ColumnName = [/SIZE][SIZE=2][COLOR=#800000]"id"
[/COLOR][/SIZE][SIZE=2]column.ReadOnly = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]column.Unique = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Add the Column to the DataColumnCollection.
[/COLOR][/SIZE][SIZE=2]table.Columns.Add(column)
[/SIZE][SIZE=2][COLOR=#008000]' Create second column.
[/COLOR][/SIZE][SIZE=2]column = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataColumn()
column.DataType = System.Type.GetType([/SIZE][SIZE=2][COLOR=#800000]"System.String"[/COLOR][/SIZE][SIZE=2])
column.ColumnName = [/SIZE][SIZE=2][COLOR=#800000]"ParentItem"
[/COLOR][/SIZE][SIZE=2]column.AutoIncrement = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]column.Caption = [/SIZE][SIZE=2][COLOR=#800000]"ParentItem"
[/COLOR][/SIZE][SIZE=2]column.ReadOnly = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]column.Unique = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Add the column to the table.
[/COLOR][/SIZE][SIZE=2]table.Columns.Add(column)
[/SIZE][SIZE=2][COLOR=#008000]' Make the ID column the primary key column.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] PrimaryKeyColumns(0) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataColumn
PrimaryKeyColumns(0) = table.Columns([/SIZE][SIZE=2][COLOR=#800000]"id"[/COLOR][/SIZE][SIZE=2])
table.PrimaryKey = PrimaryKeyColumns
[/SIZE][SIZE=2][COLOR=#008000]' Create three new DataRow objects and add 
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' them to the DataTable
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer

[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 2[INDENT]row = table.NewRow()
row(
[/INDENT][/SIZE][INDENT][SIZE=2][COLOR=#800000]"id"[/COLOR][/SIZE][SIZE=2]) = i
row([/SIZE][SIZE=2][COLOR=#800000]"ParentItem"[/COLOR][/SIZE][SIZE=2]) = [/SIZE][SIZE=2][COLOR=#800000]"ParentItem "[/COLOR][/SIZE][SIZE=2] + i.ToString()
table.Rows.Add(row)
[/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] i
table.WriteXml(Application.StartupPath & [/SIZE][SIZE=2][COLOR=#800000]"/myXML.xml"[/COLOR][/SIZE][SIZE=2])
[/SIZE]
 
Back
Top