473,388 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to read/write XML files using VB.NET

Hi,

I have been reading various messages in this group, and could not find
an answer regarding what I am trying to do. At least, I could not find
an easy answer.

I am trying to figure out how to read and XML file and put the data in
fields in a VB application, and also how to write the information to
an XML file (either to a new file or modifying an existing file.

I saw an example in a book (Leaning VB.NET 2nd edition, published by
WROX, ISBN # 0-7645-4384-9), but the examples only show a simple XML
format, and not something where you would have many levels and
sub-levels of data.

My knowledge of VB is somewhat OK. I am trying to write a program to
enhance my knowledge, and to make my job easier. :-)

I have a file similar to this.
<?xml version="1.0" encoding="UTF-8"?>

<Packagename>
<ApplicationID>
<EmployeeNo>0001</EmployeeNo>
<Lastname>Mustard</Lastname>
<Firstname>Bob</Firstname>
<Address1>
<Street>105 Main Street</Street>
<City>Toronto</City>
<Province>Ontario</Province>
<Phone>
<AreaCode>905</AreaCode>
<Number>123-4567</Number>
</Phone>
</Address1>
<Address2>
<Street>PO Box 666</Street>
<City>Toronto</City>
<Province>Ontario</Province>
<Phone>
<AreaCode/
<Number/>
</Phone>
</Address2>
<Questions>
<QuestionNo>1</QuestionNo>
<QuestionDesc>Are you smart?</QuestionDesc>
<QuestionAnswer>Yep</QuestionAnswer>
<QuestionNo>2</QuestionNo>
<QuestionDesc>Do you know VB</QuestionDesc>
<QuestionAnswer>Yep</QuestionAnswer>
<QuestionNo>3</QuestionNo>
<QuestionDesc>Do you know C++?</QuestionDesc>
<QuestionAnswer>Nope</QuestionAnswer
</Questions>
</ApplicationID>
</Packagename>

What I want to do is:
- Read the XML file, and populate the fields on a VB form.
- Review the data and either modify and save the data or do nothing
with it.

or

- Create a new file using values entered on the screen.

I am not trying to do anything complicated. I am just trying to create
a small front end to allow me to view/enter/edit data in a simple XML
document.
We use XML file for testing, and I would prefer to use a VB program
instead of modifying the XML file manually.

Anything thing I would do in the VB program is to check the data for
accuracy

If anyone could help, it would be greatly appreciated.

Thank you,

André
Nov 21 '05 #1
3 34959
Check out the MSXML DOM.

http://msdn.microsoft.com/library/de...ae3597eb0c.asp

Tom P.
"Testguy" <te*****@magma.ca> wrote in message
news:79********************************@4ax.com...
Hi,

I have been reading various messages in this group, and could not find
an answer regarding what I am trying to do. At least, I could not find
an easy answer.

I am trying to figure out how to read and XML file and put the data in
fields in a VB application, and also how to write the information to
an XML file (either to a new file or modifying an existing file.

I saw an example in a book (Leaning VB.NET 2nd edition, published by
WROX, ISBN # 0-7645-4384-9), but the examples only show a simple XML
format, and not something where you would have many levels and
sub-levels of data.

My knowledge of VB is somewhat OK. I am trying to write a program to
enhance my knowledge, and to make my job easier. :-)

I have a file similar to this.
<?xml version="1.0" encoding="UTF-8"?>

<Packagename>
<ApplicationID>
<EmployeeNo>0001</EmployeeNo>
<Lastname>Mustard</Lastname>
<Firstname>Bob</Firstname>
<Address1>
<Street>105 Main Street</Street>
<City>Toronto</City>
<Province>Ontario</Province>
<Phone>
<AreaCode>905</AreaCode>
<Number>123-4567</Number>
</Phone>
</Address1>
<Address2>
<Street>PO Box 666</Street>
<City>Toronto</City>
<Province>Ontario</Province>
<Phone>
<AreaCode/
<Number/>
</Phone>
</Address2>
<Questions>
<QuestionNo>1</QuestionNo>
<QuestionDesc>Are you smart?</QuestionDesc>
<QuestionAnswer>Yep</QuestionAnswer>
<QuestionNo>2</QuestionNo>
<QuestionDesc>Do you know VB</QuestionDesc>
<QuestionAnswer>Yep</QuestionAnswer>
<QuestionNo>3</QuestionNo>
<QuestionDesc>Do you know C++?</QuestionDesc>
<QuestionAnswer>Nope</QuestionAnswer
</Questions>
</ApplicationID>
</Packagename>

What I want to do is:
- Read the XML file, and populate the fields on a VB form.
- Review the data and either modify and save the data or do nothing
with it.

or

- Create a new file using values entered on the screen.

I am not trying to do anything complicated. I am just trying to create
a small front end to allow me to view/enter/edit data in a simple XML
document.
We use XML file for testing, and I would prefer to use a VB program
instead of modifying the XML file manually.

Anything thing I would do in the VB program is to check the data for
accuracy

If anyone could help, it would be greatly appreciated.

Thank you,

André

Nov 21 '05 #2
Hi Henry,

Thanks for the info. Unfortunatelly, this is a bit too complicated for
me. I know VB, but not at the advanced level... yet.

I was lookign for something simpler, since was I want to do is just
read and write and XML file. Nothing ancy.

Thanks anyway,

André

On Thu, 02 Jun 2005 14:18:30 GMT, "Henry Padilla"
<pa******@hotmail.com> wrote:
Check out the MSXML DOM.

http://msdn.microsoft.com/library/de...ae3597eb0c.asp

Tom P.
"Testguy" <te*****@magma.ca> wrote in message
news:79********************************@4ax.com.. .
Hi,

I have been reading various messages in this group, and could not find
an answer regarding what I am trying to do. At least, I could not find
an easy answer.

I am trying to figure out how to read and XML file and put the data in
fields in a VB application, and also how to write the information to
an XML file (either to a new file or modifying an existing file.

I saw an example in a book (Leaning VB.NET 2nd edition, published by
WROX, ISBN # 0-7645-4384-9), but the examples only show a simple XML
format, and not something where you would have many levels and
sub-levels of data.

My knowledge of VB is somewhat OK. I am trying to write a program to
enhance my knowledge, and to make my job easier. :-)

I have a file similar to this.
<?xml version="1.0" encoding="UTF-8"?>

<Packagename>
<ApplicationID>
<EmployeeNo>0001</EmployeeNo>
<Lastname>Mustard</Lastname>
<Firstname>Bob</Firstname>
<Address1>
<Street>105 Main Street</Street>
<City>Toronto</City>
<Province>Ontario</Province>
<Phone>
<AreaCode>905</AreaCode>
<Number>123-4567</Number>
</Phone>
</Address1>
<Address2>
<Street>PO Box 666</Street>
<City>Toronto</City>
<Province>Ontario</Province>
<Phone>
<AreaCode/
<Number/>
</Phone>
</Address2>
<Questions>
<QuestionNo>1</QuestionNo>
<QuestionDesc>Are you smart?</QuestionDesc>
<QuestionAnswer>Yep</QuestionAnswer>
<QuestionNo>2</QuestionNo>
<QuestionDesc>Do you know VB</QuestionDesc>
<QuestionAnswer>Yep</QuestionAnswer>
<QuestionNo>3</QuestionNo>
<QuestionDesc>Do you know C++?</QuestionDesc>
<QuestionAnswer>Nope</QuestionAnswer
</Questions>
</ApplicationID>
</Packagename>

What I want to do is:
- Read the XML file, and populate the fields on a VB form.
- Review the data and either modify and save the data or do nothing
with it.

or

- Create a new file using values entered on the screen.

I am not trying to do anything complicated. I am just trying to create
a small front end to allow me to view/enter/edit data in a simple XML
document.
We use XML file for testing, and I would prefer to use a VB program
instead of modifying the XML file manually.

Anything thing I would do in the VB program is to check the data for
accuracy

If anyone could help, it would be greatly appreciated.

Thank you,

André


Nov 21 '05 #3
Hi,
the XML support is huge in .NET so it's easy to get
lost as a beginner. There is however more than
enough information if you search hard enough.
I have some code I can copy and paste
for you to show you XML serialization, it's as simple
as it can get. The XML serializer creates a XML
file out of a object or the other way around,
you do not parse or create the XML by hand.

code requires VS 2005 because of generics,
you can download it at MSDN for free!

Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Text

Partial Public Class MainForm
Public Sub New()
InitializeComponent()

Dim filepath As String = Path.Combine(Application.StartupPath, "Settings.xml")

If File.Exists(filepath) Then
g.Settings = g.LoadXML(Of ApplicationSettings)(filepath)
End If
End Sub

Private Sub MainForm_FormClosing(ByVal sender As System.Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
g.SaveXML(Path.Combine(Application.StartupPath, "Settings.xml"), g.Settings)
End Sub
End Class

Public Class g
Public Shared Settings As New ApplicationSettings

Public Shared Sub SaveXML(ByVal path As String, ByVal obj As Object)
Dim s As XmlSerializer = New XmlSerializer(obj.GetType)

Using w As New XmlTextWriter(path, Encoding.UTF8)
w.Formatting = Formatting.Indented
s.Serialize(w, obj)
End Using
End Sub

Public Shared Function LoadXML(Of T)(ByVal path As String) As T
Dim s As XmlSerializer = New XmlSerializer(GetType(T))

Using r As New XmlTextReader(path)
Return CType(s.Deserialize(r), T)
End Using
End Function
End Class

Public Class ApplicationSettings
Public Strings As New List(Of String)
'add here more settings
End Class

---------------

You can use the XMLDocument, XmlTextReader and
XmlTextWriter class to do things manually.
I'm not a experienced on the topic either.

from the .NET documentation on WriteStartElement:

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample

Public Shared Sub Main()
'Create a writer to write XML to the console.
Dim writer As XmlTextWriter = Nothing
writer = New XmlTextWriter(Console.Out)

'Use indentation for readability.
writer.Formatting = Formatting.Indented
writer.Indentation = 4

'Write an element (this one is the root).
writer.WriteStartElement("book")

'Write the title element.
writer.WriteStartElement("title")
writer.WriteString("Pride And Prejudice")
writer.WriteEndElement()

'Write the close tag for the root element.
writer.WriteEndElement()

'Write the XML to file and close the writer.
writer.Close()
End Sub 'Main
End Class 'Sample

--------------------------

this one is C#, sorry, I hope you can read it,
it's not that much different from VB. It reads
a XML file...

string path = Path.Combine(Application.StartupPath, "Items.xml");

if (File.Exists(path))
{
XmlDocument d = new XmlDocument();
d.Load(path);

foreach (XmlElement element in d.DocumentElement.ChildNodes)
{
string attrib = element.GetAttribute("name")

string content = null;

if (element.Name == "a")
{
content = element.InnerText;
}
}
}
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Pyder | last post by:
Hi I'm trying to find informations regarding how to read SRTM file (Shuttle Radar Topographics Mission) using VB. Please, Could you help me ?? Thanks in advance
22
by: Matthias Braun | last post by:
Help! I am using SuSE 9.3, php5 and Apache2 on Linux. I try to write files to the hard drive using the following command: $filename = "test.txt"; if (!file_exists($filename))...
1
by: Aries | last post by:
Hi I need to write programs to read a file and process it with C# in ASP. I need to use C# because other team members which will be joining our team are using C#. Can anyone please show me how...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
4
by: Neil Sutton via .NET 247 | last post by:
Hi, I am new to Visual Basic .Net (some experience with VB6) and I amtrying to write my first application for my PocketPC (WM2003) Ineed to be able to create/store/recall records like a...
1
by: vinothg | last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and...
2
by: asenthil | last post by:
i'm trying to read and write files using java... some errors occurs when i'm trying this code.. Error in java: Cannot find symbol location: class java.io.FileOutputStream FileOutputStream...
0
by: Prasun Ghoshal | last post by:
I want to write a program that reads input files from a specified location. The application will run for every 2 hours and read any number of files and merged all the records one by one and prepared...
11
by: titan nyquist | last post by:
I want to read/write user settings. I thought about using .ini or .xml files. Are there classes in c# to handle this? The stuff I find on the 'net seems old. Titan
6
by: =?Utf-8?B?LnBhdWwu?= | last post by:
how can i write to an excel .xls file using odbc? i've read in several places that its possible but i can't find an example. i have managed to read an excel file using odbc
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.