Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

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

VB.NET reading xml files useful for database xml for connectionstring xmlreader xml xml files

  • Please log in to reply
1 reply to this topic

#1 papabear


    CC Devotee

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

Posted 21 April 2012 - 04:53 AM

this is a continuation tutorial, you can always access the first part Writing and Creating XML Files

Writing and reading xml files

- Creating and Writing into XML files
- Reading XML files

in this part 2 of my tutorial we will study on how to read what is inside the xml files, we are still going to use the project that we've created at the part I. In the first part we use the form load event to create a xml file and at the same time write something into it. In this tutorial we will be using the button1 click event.

Double click the button1 in design view. You should see something like this

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

	End Sub

Inside that subroutine paste this code

		'check if file myxml.xml is existing
		If (IO.File.Exists("MyXML.xml")) Then

			'create a new xmltextreader object
			'this is the object that we will loop and will be used to read the xml file
			Dim document As XmlReader = New XmlTextReader("MyXML.xml")

			'loop through the xml file
			While (document.Read())

				Dim type = document.NodeType

				'if node type was element
				If (type = XmlNodeType.Element) Then

					'if the loop found a <FirstName> tag
					If (document.Name = "FirstName") Then

						TextBox1.Text = document.ReadInnerXml.ToString()

					End If

					'if the loop found a <LastName tag
					If (document.Name = "LastName") Then

						TextBox2.Text = document.ReadInnerXml.ToString()

					End If

				End If

			End While


			MessageBox.Show("The filename you selected was not found.")
		End If

explanations are inside the code above and commented :) it's easy to follow right?
This kind of technique was useful because you can use it to store your settings, connectionstrings for database connection, save username/password for textbox "Remember me" something like that? you can simply read and write into it if ever you want to change the settings, you can just open the xml file whenever and wherever you want without opening the source code of the program. I will be attaching the complete program below. Feel free to download it and make some discovery and experiments :)

happy coding

Attached File  xmlPractice.zip   73.31KB   3773 downloads
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#2 pgkdave


    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 31 August 2014 - 04:00 AM

Hi, here is my challenge, I want create a news edit form that uses the attached  attached XML file and XSLT.  The example I am looking at (http://demos.devexpr...ing.aspx) shows how to read the XML file and display the News article but doesn't advise how to  add new articles or edit or delete the existing articles ones.  From looking at the code, I see that the images are pulled from a NewsControl folder.  I have never used XML before so any help and guidance as to how I can  modify this example to upload the image to the NewsControl folder and add the image to the xml file would be greatly appreciated.

Attached Files

  • 0

Also tagged with one or more of these keywords: VB.NET, reading xml files, useful for database, xml for connectionstring, xmlreader, xml, xml files

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download