Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
  • Photo
      11 Aug
    Error

    Should I be practicing programming every day? I feel if I don't, I'll get instantly rusty or something.

    Show comments (4)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Writing And Reading Xml Files (Vb.net) Part I

VB.NET reading xml writing xml xml as database vb.net visual studio create xml xml and vb.net xml xml files

  • Please log in to reply
8 replies to this topic

#1 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 471 posts
  • Location:DarkSide

Posted 13 April 2012 - 08:31 PM

Hello everyone, how are you doing? I will be posting my second tutorial now and the topic that I'm going to cover in this tutorial are :

- 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"

Posted Image


now we have a new form named "Form1.vb"

now create 2 labels, 2 textbox and 1 button and make them something like this

Posted Image

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.

Posted Image


- 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

Posted Image

- 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..

Posted Image

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.


Update :
Part 2 is finish you can access it here "Reading XML files"

Have Fun :)
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#2 LloydTov

LloydTov

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C
  • Learning:C

Posted 13 November 2012 - 01:44 AM

Hi there,
When using this code i get an error on this line:

Dim xmlwrt As XmlWriter = XmlWriter.Create("DaySum.xml")

It gives me a bunch of errors but this is the top one:

Error 1 Overload resolution failed because no accessible 'Create' can be called with these arguments:

Any help would be appreciated
Cheers
  • 0

#3 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 471 posts
  • Location:DarkSide

Posted 13 November 2012 - 03:48 AM

Hi there,
When using this code i get an error on this line:

Dim xmlwrt As XmlWriter = XmlWriter.Create("DaySum.xml")

It gives me a bunch of errors but this is the top one:

Error 1 Overload resolution failed because no accessible 'Create' can be called with these arguments:

Any help would be appreciated
Cheers


please make sure that you have imported the required library :)
can you give me your code? thank you so much for reading my tutorial
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#4 LloydTov

LloydTov

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C
  • Learning:C

Posted 14 November 2012 - 02:21 AM

urm yeah.... Imports.system and imports.system.Xml??
heres the code, thanks again :)

Dim xmlwrt As XmlWriter = XmlWriter.Create("DaySum.xml")

With xmlwrt
.WriteStartDocument()
' Writes the xml declaration
.WriteComment("Day Summary")
' Writes the title

.WriteStartElement("Total takings for the Day - ")
' Writes a sub heading
.WriteStartElement(TextBox1.Text.ToString)
' Writes the content of text box 1 to file
.WriteEndElement()
' Ends this line

MessageBox.Show("File Saved")
' Shows a message box telling the user the file has been saved
  • 0

#5 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 471 posts
  • Location:DarkSide

Posted 14 November 2012 - 03:02 AM

urm yeah.... Imports.system and imports.system.Xml??
heres the code, thanks again :)

Dim xmlwrt AsXmlWriter = XmlWriter.Create("DaySum.xml")

With xmlwrt
.WriteStartDocument()
' Writes the xml declaration
.WriteComment("Day Summary")
' Writes the title

.WriteStartElement("Total takings for the Day - ")
' Writes a sub heading
.WriteStartElement(TextBox1.Text.ToString)
' Writes the content of text box 1 to file
.WriteEndElement()
' Ends this line

MessageBox.Show("File Saved")
' Shows a message box telling the user the file has been saved


the

imports system and
imports system.xml

doesn't have period or dot (.) between imports system

Imports.system and imports.system.Xml


  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#6 LloydTov

LloydTov

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C
  • Learning:C

Posted 14 November 2012 - 03:14 AM

Yeah sorry typed that out wrong. I have done that part correctly and it still gives me the error :/
  • 0

#7 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 471 posts
  • Location:DarkSide

Posted 15 November 2012 - 04:49 AM

Yeah sorry typed that out wrong. I have done that part correctly and it still gives me the error :/


I wonder if you can give me your whole project? So I can try to debug it, because into your snippet >_< I can't see anything wrong or my eyes can't see them, thank you so much ^_^
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#8 LloydTov

LloydTov

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C
  • Learning:C

Posted 16 November 2012 - 09:12 AM

yeah i can do that, could you give me your wmail address so i can send it to you then? or is it this : freedom_war550@live.com.ph or freedom_war550@yahoo.com ??
ill send my project to both of them
cheers mate :)
  • 0

#9 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 471 posts
  • Location:DarkSide

Posted 16 November 2012 - 03:59 PM

yeah i can do that, could you give me your wmail address so i can send it to you then? or is it this : freedom_war550@live.com.ph or freedom_war550@yahoo.com ??
ill send my project to both of them
cheers mate :)


the live.com.ph one sir, I'll check it later.. thank you
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped





Also tagged with one or more of these keywords: VB.NET, reading xml, writing xml, xml as database, vb.net, visual studio, create xml, xml and vb.net, xml, xml files