XML Visual Basic Questions

G

Guest

First let me say I am familar with programming, but not familiar with VB.net.

I am loading an existing XML document with this procedure

Dim xmlFile As String = "..\data\Products.xml"
Dim xmlDoc As XmlDocument

Private Sub Zadig_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim xmlTr As New XmlTextReader(xmlFile)

While xmlTr.Read
If xmlTr.Name = "id" AndAlso xmlTr.NodeType =
XmlNodeType.Element Then
cmbProducts.Items.Add(xmlTr.ReadString)
End If
End While
xmlTr.Close()

xmlDoc = New XmlDocument
xmlDoc.Load(xmlFile)

End Sub

If I have a NEW form (IE-Products.vb), will I be able to reference the XML
doc in Memory?

Also, is there a good web reference that shows how to update
XML data (In memory) and then write them back out to a file?

Thanks,

Michael
 
C

Cor Ligthert

Fremenusul.

When you start with VBNet or beter dotNet and will use XML, you have
yourself first is this an data file or a document.

When it is a data file you would first have a look at the dataset, because
that has its own methods to handle elements, which are a lot easier than the
loaddoc and the xmlreader.

When you have more questions, please reply

Cor
 
S

Sean Hederman

Each instance of form Zadig will have it's own copy of the xmlDoc variable,
but other form types will not. To share the xmlDoc variable amoungst forms
will require that you make it Public. A good way of exposing it would be the
Singleton Pattern:

Public Class Settings

Private Shared xmlDoc As XmlDocument

Public Shared Property Products As XmlDocument
Get
If xmlDoc Is Nothing Then
' Initialize & load xmlDoc here
End If

Return xmlDoc
End Get
End Property

End Class

Then to access the XmlDocument you'd just do:
Settings.Products

Have a look at
http://samples.gotdotnet.com/quickstart/howto/doc/xml/overviewofxml.aspx for
more information on using XML in an application.
 
G

Guest

Why is datafile better? I am learning and want to understand the "why's" as
much as the "how's".

Thanks,

Michael
 
P

Patrice

It's just about the programming model. Instead of seing your document as an
XML file you'll see it at a collection of rows with columns as if it were
coming from a DB...

Patrice

--
 
C

Cor Ligthert

Fremesul,

I checked it because I was surprised that I wrote "better". There is in my
opinion no "best" solution, every solution has a better alternative and I
don't know if that is in this case. I wrote "easier".

However, the dataset is made to cooperate with SQL based databaseservers and
has therefore every functionality to handle it.

Basically it exist from collections of datatables, which have collections
of datarows, which have collections of dataitems. The dataitems are
described in datacolumns. While as well it is possible to make relations in
this for the tables.

It has a lot of standard methods in dotnet to send and use in different
ways.

To give you four samples in VBNet format,

reading of a dataset from a disk is.
mydataset.readXML("path").
writing
mydataset.writeXML("path")
showing the complete dataset in a datagrid
mydatagrid1.datasource = mydataset
getting the first item from the first row from the first table
mydataset.tables(0).rows(0)(0)
or when it would have had names
mydataset.tables("myfirsttable").rows(0).items("MyFirstItem")

However, this is just the begin and all based on data.

I hope this gives some ideas'

Cor
 
C

Cor Ligthert

Patrice,
It's just about the programming model. Instead of seing your document as
an
XML file you'll see it at a collection of rows with columns as if it were
coming from a DB...

In my opinion wrong; every dataset can be a XML file, not every XML file is
a dataset.

Cor
 
P

Patrice

Is this what I said ?

Was just trying to respond to fremensul that wanted to know why you
suggested to use a DataSet for XML data files. My remakrs about the
programming model is to be taken in this context, not that you should always
handle all XML files on earth using this programming model...

Patrice
--
 
C

Cor Ligthert

Patrice,

Je pense que c'était mon mauvaise idée au sujet de votre texte.
I think that it was my wrong idea about your text.

Excusez-moi.
Sorry

:)

Cor
 
P

Patrice

Sorry, it sometimes a bit hard to express yourself or to understand what
others are saying. I even wondered before answering as this is not so
important fbut found a bit strange your response in this context.

Later in this group - you saw English is not my native language ;-)

Patrice
 
G

Guest

Thanks to everyone that replied. I will be reading over all the answers and
most likley, asking lots more questions.

Thanks again.
 
G

Guest

A followup question...

If I use XmlDocument (DOM implimentation I believe) I can "append" data to
my XML file right (In memory). Then when I am done, I can "output" a xml file.

Is there somewhere that can explain this process clearly? Read in XML file,
modify the xml (in memory) then output it back out???

Thanks for any help.
 

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