How to read/write XML files using VB.NET

T

Testguy

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é
 
T

Testguy

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é
 
F

Frank Skare

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;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top