- Writing XML files
- Reading XML files
and I'm going to provide some screenshot for my tutorial to be friendly so that everyone will have no problem or less problem. Feel free give me some suggestions or leave some comments regarding this tutorial
Let's start now by creating a new windows form application project and name it "xmlPractice"
now we have a new form named "Form1.vb"
now create 2 labels, 2 textbox and 1 button and make them something like this
now we have textbox1, textbox2, label1, label2 and button1
here's the property for the label1
Text = First Name
property for the label2
Text = Last Name
property for textbox1
Name = txtFirstName
property for textbox2
Name = txtLastName
property for button1
Name = btnView
Text = View Mysterious Text!
if you don't know how to this properties, you might want to check this tutorials provided by our codecall friends
VB.net from beginner to advance by Vswe.
Now that the form that we are going to use is all set, we are now going to proceed with xml definition and what you can do with it.
What is XML?
Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. It is defined in the XML 1.0 Specificationproduced by the W3C, and several other related specifications, all gratis open standards. - wikipedia
What can I do with XML?
You can use xml to store some data like application settings or database connection strings if you are making something like a generic database driven software where you can just edit the XML file to change the xml file. You can also use this XML to save state
ok done with the definition, let's start.
Creating and Writing into a XML File
- Double click the Form1 and you should see something like this.
- Add the this code in the before the "Public Class Form1"
Imports System Imports System.Xml
- Go to design view and double click the form again, Inside the Form load event put this code
'first let's check if there is a file MyXML.xml into our application folder 'if there wasn't a file something like that, then let's create a new one. If IO.File.Exists("MyXML.xml") = False Then 'declare our xmlwritersettings object Dim settings As New XmlWriterSettings() 'lets tell to our xmlwritersettings that it must use indention for our xml settings.Indent = True 'lets create the MyXML.xml document, the first parameter was the Path/filename of xml file ' the second parameter was our xml settings Dim XmlWrt As XmlWriter = XmlWriter.Create("MyXML.xml", settings) With XmlWrt ' Write the Xml declaration. .WriteStartDocument() ' Write a comment. .WriteComment("XML Database.") ' Write the root element. .WriteStartElement("Data") ' Start our first person. .WriteStartElement("Person") ' The person nodes. .WriteStartElement("FirstName") .WriteString("Alleo") .WriteEndElement() .WriteStartElement("LastName") .WriteString("Indong") .WriteEndElement() ' The end of this person. .WriteEndElement() ' Close the XmlTextWriter. .WriteEndDocument() .Close() End With MessageBox.Show("XML file saved.") End If
the current code so far will look something like this
- when you run your code, you should be able to see your xml file into where ever
you save your application. In my case my default location for my subjects is here
C:\Documents and Settings\Alleo Indong\My Documents\Visual Studio 2010\Projects\xmlPractice\xmlPractice\bin\Debug
Here's what my debug folder contains..
and when I open my "MyXML.xml" Here's the what I saw
<?xml version="1.0" encoding="utf-8"?> <!--XML Database.--> <Data> <Person> <FirstName>Alleo</FirstName> <LastName>Indong</LastName> </Person> </Data>
- Works great , Now that we can create and write values into an XML file all we need to do now is to read some information or data in it and it will be covered into my next tutorial and I will write the next one as soon as possible, so stay tuned.
Part 2 is finish you can access it here "Reading XML files"