Visual basic express, read and update Xml file

G

Guest

Hi,

I'm steel trying to read and update my XML file with Visual Basic
Express but i am unable to find the right way to read my xml file and
update it if neccessary...

Here is my problem : evry day, i store the number of children in my
classroom in my XML file. For exemple, on monday, my app ask me
something like this : msgbox ("Are the 28 children here today ?",vbyesno)

If not, I write 25 in an inputbox and i store this information in the xml.

On tuesday, I run my app and I would like to be asked :
"Are the 3 children (which where missing yesterday) back today ?"

If yes, I want to store 28 in the xml file. If not, I store the number
written
in my inputbox.

Etc...

At the end, i would like to see my xml file in a treeView and be able to
retrive all the info stored in the differents nodes (Year, month, day).

Does anybody knoes how can I do ?

Think you for your attention (and sorry for my english)...

Evelyne
 
R

rowe_newsgroups

Hi,

I'm steel trying to read and update my XML file with Visual Basic
Express but i am unable to find the right way to read my xml file and
update it if neccessary...

Here is my problem : evry day, i store the number of children in my
classroom in my XML file. For exemple, on monday, my app ask me
something like this : msgbox ("Are the 28 children here today ?",vbyesno)

If not, I write 25 in an inputbox and i store this information in the xml.

On tuesday, I run my app and I would like to be asked :
"Are the 3 children (which where missing yesterday) back today ?"

If yes, I want to store 28 in the xml file. If not, I store the number
written
in my inputbox.

Etc...

At the end, i would like to see my xml file in a treeView and be able to
retrive all the info stored in the differents nodes (Year, month, day).

Does anybody knoes how can I do ?

Think you for your attention (and sorry for my english)...

Evelyne

All the Xml related classes are in the System.Xml namespace, so you
might want to pull up object browser and look at those classes.

Here's a quick example of an update routine that sets each Child1 node
to "Hello World!":

' An Xml File

<BaseNode>
<Parent>
<Child1>InnerText</Child1>
<Child2>InnerText</Child2>
</Parent>
<Parent>
<Child1>InnerText</Child1>
<Child2>InnerText</Child2>
</Parent>
</BaseNode>

<pseudocode>

' Imports System.Xml is needed

Dim doc as new XmlDocument
doc.Load("C:\MyPath\MyXmlFile.xml")
for each node as XmlNode in doc.GetElementsByTagName("Parent")
if node.Name = "Child1" then
node.InnerText = "Hello World!"
end if
next

</pseudocode>

If you need something more specific, and can't find the answer in the
archives, please post back and I'll try to help.

Thanks,

Seth Rowe
 
G

Guest

rowe_newsgroups said:
All the Xml related classes are in the System.Xml namespace, so you
might want to pull up object browser and look at those classes.

Here's a quick example of an update routine that sets each Child1 node
to "Hello World!":

' An Xml File

<BaseNode>
<Parent>
<Child1>InnerText</Child1>
<Child2>InnerText</Child2>
</Parent>
<Parent>
<Child1>InnerText</Child1>
<Child2>InnerText</Child2>
</Parent>
</BaseNode>

<pseudocode>

' Imports System.Xml is needed

Dim doc as new XmlDocument
doc.Load("C:\MyPath\MyXmlFile.xml")
for each node as XmlNode in doc.GetElementsByTagName("Parent")
if node.Name = "Child1" then
node.InnerText = "Hello World!"
end if
next

</pseudocode>

If you need something more specific, and can't find the answer in the
archives, please post back and I'll try to help.

Thanks,

Seth Rowe

Hi,
thank you very much for your reply.

In my XML file, the informations are stored like that :

<PUPILL_CP_1>
<YEAR_2006>
<OCTOBRE EffectifDeLaClasse="28" Presents="25" Absents="3" Comment="" />
<NOVEMBRE EffectifDeLaClasse="28" Presents="28" Absents="0"
Comment="...." />
<DECEMBRE EffectifDeLaClasse="28" Presents="20" Absents="5"
Comment="blabla bla" />
</YEAR_2006>
<YEAR_2007>
<JANVIER EffectifDeLaClasse="28" Presents="10" Absents="18"
Comment="DANGER important desease"/>
<FEVRIER EffectifDeLaClasse="28" Presents="27" Absents="1"
Comment="something else" />
<MARS EffectifDeLaClasse="28" Presents="10" Absents="18"
Comment="something" />
</YEAR_2007>
</PUPILL_CP_1>

Thanks to your help, I am now able to read my xml file. Then, if it's
possible,
I would like to do the following .

1/ While reading my XML file, I would like to update the attributs of the
nodes.

For exemple, if I run my app in marsh and if my app read this
Comment="DANGER
important desease" (in the node <JANVIER EffectifDeLaClasse="28"
Presents="10"
Absents="18" Comment="DANGER important desease"/>)
I would like to have a message like :

If Msgbox("In Marsh 2007, do you know something about the desease in
<JANVIER>
<2007> ?", VbYesNo) = VbYes then inputbox("Write a new comment about the
<JANVIER>
<2007> desease.") end if
and then update the XML file and continue to read the file.

2/ Once my xml file is updated, I raise it in a TreeView. Then, when I click
on
the Node <YEAR_2006>, I would like to have a MsgBox where all the SubNodes
<OCTOBRE> <NOVEMBRE> <DÉCEMBRE> are listed.

If my explanations are clear enough, may be you know so practical solution
as the
previous you have already given to me !!!

Best regards,

Evelyne
 
R

rowe_newsgroups

"rowe_newsgroups" <[email protected]> a écrit dans le message de (e-mail address removed)...








Hi,
thank you very much for your reply.

In my XML file, the informations are stored like that :

<PUPILL_CP_1>
<YEAR_2006>
<OCTOBRE EffectifDeLaClasse="28" Presents="25" Absents="3" Comment="" />
<NOVEMBRE EffectifDeLaClasse="28" Presents="28" Absents="0"
Comment="...." />
<DECEMBRE EffectifDeLaClasse="28" Presents="20" Absents="5"
Comment="blabla bla" />
</YEAR_2006>
<YEAR_2007>
<JANVIER EffectifDeLaClasse="28" Presents="10" Absents="18"
Comment="DANGER important desease"/>
<FEVRIER EffectifDeLaClasse="28" Presents="27" Absents="1"
Comment="something else" />
<MARS EffectifDeLaClasse="28" Presents="10" Absents="18"
Comment="something" />
</YEAR_2007>
</PUPILL_CP_1>

Thanks to your help, I am now able to read my xml file. Then, if it's
possible,
I would like to do the following .

1/ While reading my XML file, I would like to update the attributs of the
nodes.

For exemple, if I run my app in marsh and if my app read this
Comment="DANGER
important desease" (in the node <JANVIER EffectifDeLaClasse="28"
Presents="10"
Absents="18" Comment="DANGER important desease"/>)
I would like to have a message like :

If Msgbox("In Marsh 2007, do you know something about the desease in
<JANVIER>
<2007> ?", VbYesNo) = VbYes then inputbox("Write a new comment about the
<JANVIER>
<2007> desease.") end if
and then update the XML file and continue to read the file.

2/ Once my xml file is updated, I raise it in a TreeView. Then, when I click
on
the Node <YEAR_2006>, I would like to have a MsgBox where all the SubNodes
<OCTOBRE> <NOVEMBRE> <DÉCEMBRE> are listed.

If my explanations are clear enough, may be you know so practical solution
as the
previous you have already given to me !!!

Best regards,

Evelyne

Getting attributes is fairly easier, XmlNode has a property called
Attributes that is a collection of node's attributes. You can do
something like this to get the comment out of the JANVIER node in
<YEAR_2007>

<pseudocode>

Dim doc as new XmlDocument()
doc.Load("MyXml.xml")
for each node as XmlNode in doc.GetElementsByTagName("YEAR_2007")
if node.Name = "JANVIER" then
' Show the value of the comments attribute
MessageBox.Show(node.Attributes("comments").Value.ToString())
' Update it's value
node.Attributes("comments").Value = "Hello World!"
end if
next

</pseudocode>

That should take care of the attributes. On to the TreeView!

If I understood correctly, you want to use a TreeView to mimic the
layout of the Xml file right?

I dropped your Xml into a file on my desktop and wrote a quick app
that loads the nodes and attributes into a TreeView (called TreeView1)
on a form. Let me know how it works!

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim doc As New XmlDocument()
doc.Load("C:\Documents and Settings\srowe\Desktop\test.xml")
Dim node As XmlNode = doc.ChildNodes(0)
Dim tNode As New TreeNode(node.Name)
Me.TreeView1.Nodes.Add(tNode)
For Each child As XmlNode In node.ChildNodes
AddNodetoTreeview(child, tNode)
Next
End Sub

Private Sub AddNodetoTreeview(ByVal node As XmlNode, ByVal parent
As TreeNode)
Dim tNode As New TreeNode(node.Name)
parent.Nodes.Add(tNode)
For Each n As XmlNode In node.ChildNodes
AddNodetoTreeview(n, tNode)
Next
For Each a As XmlAttribute In node.Attributes
tNode.Nodes.Add(String.Format("{0} - {1}", a.Name,
a.Value))
Next
End Sub

Thanks,

Seth Rowe
 
E

E.F

"rowe_newsgroups" <[email protected]> a écrit dans le message de (e-mail address removed)...
"rowe_newsgroups" <[email protected]> a écrit dans le message de (e-mail address removed)...








Hi,
thank you very much for your reply.

In my XML file, the informations are stored like that :

<PUPILL_CP_1>
<YEAR_2006>
<OCTOBRE EffectifDeLaClasse="28" Presents="25" Absents="3" Comment=""
/>
<NOVEMBRE EffectifDeLaClasse="28" Presents="28" Absents="0"
Comment="...." />
<DECEMBRE EffectifDeLaClasse="28" Presents="20" Absents="5"
Comment="blabla bla" />
</YEAR_2006>
<YEAR_2007>
<JANVIER EffectifDeLaClasse="28" Presents="10" Absents="18"
Comment="DANGER important desease"/>
<FEVRIER EffectifDeLaClasse="28" Presents="27" Absents="1"
Comment="something else" />
<MARS EffectifDeLaClasse="28" Presents="10" Absents="18"
Comment="something" />
</YEAR_2007>
</PUPILL_CP_1>

Thanks to your help, I am now able to read my xml file. Then, if it's
possible,
I would like to do the following .

1/ While reading my XML file, I would like to update the attributs of the
nodes.

For exemple, if I run my app in marsh and if my app read this
Comment="DANGER
important desease" (in the node <JANVIER EffectifDeLaClasse="28"
Presents="10"
Absents="18" Comment="DANGER important desease"/>)
I would like to have a message like :

If Msgbox("In Marsh 2007, do you know something about the desease in
<JANVIER>
<2007> ?", VbYesNo) = VbYes then inputbox("Write a new comment about the
<JANVIER>
<2007> desease.") end if
and then update the XML file and continue to read the file.

2/ Once my xml file is updated, I raise it in a TreeView. Then, when I
click
on
the Node <YEAR_2006>, I would like to have a MsgBox where all the
SubNodes
<OCTOBRE> <NOVEMBRE> <DÉCEMBRE> are listed.

If my explanations are clear enough, may be you know so practical solution
as the
previous you have already given to me !!!

Best regards,

Evelyne

Getting attributes is fairly easier, XmlNode has a property called
Attributes that is a collection of node's attributes. You can do
something like this to get the comment out of the JANVIER node in
<YEAR_2007>

<pseudocode>

Dim doc as new XmlDocument()
doc.Load("MyXml.xml")
for each node as XmlNode in doc.GetElementsByTagName("YEAR_2007")
if node.Name = "JANVIER" then
' Show the value of the comments attribute
MessageBox.Show(node.Attributes("comments").Value.ToString())
' Update it's value
node.Attributes("comments").Value = "Hello World!"
end if
next

</pseudocode>

That should take care of the attributes. On to the TreeView!

If I understood correctly, you want to use a TreeView to mimic the
layout of the Xml file right?

I dropped your Xml into a file on my desktop and wrote a quick app
that loads the nodes and attributes into a TreeView (called TreeView1)
on a form. Let me know how it works!

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim doc As New XmlDocument()
doc.Load("C:\Documents and Settings\srowe\Desktop\test.xml")
Dim node As XmlNode = doc.ChildNodes(0)
Dim tNode As New TreeNode(node.Name)
Me.TreeView1.Nodes.Add(tNode)
For Each child As XmlNode In node.ChildNodes
AddNodetoTreeview(child, tNode)
Next
End Sub

Private Sub AddNodetoTreeview(ByVal node As XmlNode, ByVal parent
As TreeNode)
Dim tNode As New TreeNode(node.Name)
parent.Nodes.Add(tNode)
For Each n As XmlNode In node.ChildNodes
AddNodetoTreeview(n, tNode)
Next
For Each a As XmlAttribute In node.Attributes
tNode.Nodes.Add(String.Format("{0} - {1}", a.Name,
a.Value))
Next
End Sub

Thanks,

Seth Rowe
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Hi,

one more time, I am very grateful to you !

Thanks to your previous explanation, I can display all the attributs
of my elements in my treeview.

But now, I need to be able to modify or to update those attributs, not
directly on the treeview, but with an update function.

To do that, I have written a procedure which allows me, each time y drop
my treeview, to put the values of the attributs in a string whitch is added
to the tag of the node .

Then, when I click on one node, with the split function, each element of the
string (stored in the tag) are displayed in differents textboxes
corresponding
with each attributes of my treewiew.

But I am afraid of not using the more safe or appropriate function... In
this case
and if I have managed to be understood, may be know you some lines of code
which will allow me to reach my purpose ?

Thanks for your attention,

Evelyne
 
R

rowe_newsgroups

"rowe_newsgroups" <[email protected]> a écrit dans le message de (e-mail address removed)...













Getting attributes is fairly easier, XmlNode has a property called
Attributes that is a collection of node's attributes. You can do
something like this to get the comment out of the JANVIER node in
<YEAR_2007>

<pseudocode>

Dim doc as new XmlDocument()
doc.Load("MyXml.xml")
for each node as XmlNode in doc.GetElementsByTagName("YEAR_2007")
if node.Name = "JANVIER" then
' Show the value of the comments attribute
MessageBox.Show(node.Attributes("comments").Value.ToString())
' Update it's value
node.Attributes("comments").Value = "Hello World!"
end if
next

</pseudocode>

That should take care of the attributes. On to the TreeView!

If I understood correctly, you want to use a TreeView to mimic the
layout of the Xml file right?

I dropped your Xml into a file on my desktop and wrote a quick app
that loads the nodes and attributes into a TreeView (called TreeView1)
on a form. Let me know how it works!

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim doc As New XmlDocument()
doc.Load("C:\Documents and Settings\srowe\Desktop\test.xml")
Dim node As XmlNode = doc.ChildNodes(0)
Dim tNode As New TreeNode(node.Name)
Me.TreeView1.Nodes.Add(tNode)
For Each child As XmlNode In node.ChildNodes
AddNodetoTreeview(child, tNode)
Next
End Sub

Private Sub AddNodetoTreeview(ByVal node As XmlNode, ByVal parent
As TreeNode)
Dim tNode As New TreeNode(node.Name)
parent.Nodes.Add(tNode)
For Each n As XmlNode In node.ChildNodes
AddNodetoTreeview(n, tNode)
Next
For Each a As XmlAttribute In node.Attributes
tNode.Nodes.Add(String.Format("{0} - {1}", a.Name,
a.Value))
Next
End Sub

Thanks,

Seth Rowe
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Hi,

one more time, I am very grateful to you !

Thanks to your previous explanation, I can display all the attributs
of my elements in my treeview.

But now, I need to be able to modify or to update those attributs, not
directly on the treeview, but with an update function.

To do that, I have written a procedure which allows me, each time y drop
my treeview, to put the values of the attributs in a string whitch is added
to the tag of the node .

Then, when I click on one node, with the split function, each element of the
string (stored in the tag) are displayed in differents textboxes
corresponding
with each attributes of my treewiew.

But I am afraid of not using the more safe or appropriate function... In
this case
and if I have managed to be understood, may be know you some lines of code
which will allow me to reach my purpose ?

Thanks for your attention,

Evelyne

I'm not sure I follow. Do you have a procedure that does what you need
and just aren't sure if it's the best way? If so please post the code
and I'll be happy to look at it.

Thanks,

Seth Rowe
 
G

Guest

"rowe_newsgroups" <[email protected]> a écrit dans le message de (e-mail address removed)...
"rowe_newsgroups" <[email protected]> a écrit dans le message de (e-mail address removed)...













Getting attributes is fairly easier, XmlNode has a property called
Attributes that is a collection of node's attributes. You can do
something like this to get the comment out of the JANVIER node in
<YEAR_2007>

<pseudocode>

Dim doc as new XmlDocument()
doc.Load("MyXml.xml")
for each node as XmlNode in doc.GetElementsByTagName("YEAR_2007")
if node.Name = "JANVIER" then
' Show the value of the comments attribute
MessageBox.Show(node.Attributes("comments").Value.ToString())
' Update it's value
node.Attributes("comments").Value = "Hello World!"
end if
next

</pseudocode>

That should take care of the attributes. On to the TreeView!

If I understood correctly, you want to use a TreeView to mimic the
layout of the Xml file right?

I dropped your Xml into a file on my desktop and wrote a quick app
that loads the nodes and attributes into a TreeView (called TreeView1)
on a form. Let me know how it works!

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim doc As New XmlDocument()
doc.Load("C:\Documents and Settings\srowe\Desktop\test.xml")
Dim node As XmlNode = doc.ChildNodes(0)
Dim tNode As New TreeNode(node.Name)
Me.TreeView1.Nodes.Add(tNode)
For Each child As XmlNode In node.ChildNodes
AddNodetoTreeview(child, tNode)
Next
End Sub

Private Sub AddNodetoTreeview(ByVal node As XmlNode, ByVal parent
As TreeNode)
Dim tNode As New TreeNode(node.Name)
parent.Nodes.Add(tNode)
For Each n As XmlNode In node.ChildNodes
AddNodetoTreeview(n, tNode)
Next
For Each a As XmlAttribute In node.Attributes
tNode.Nodes.Add(String.Format("{0} - {1}", a.Name,
a.Value))
Next
End Sub

Thanks,

Seth Rowe
'"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Hi,

one more time, I am very grateful to you !

Thanks to your previous explanation, I can display all the attributs
of my elements in my treeview.

But now, I need to be able to modify or to update those attributs, not
directly on the treeview, but with an update function.

To do that, I have written a procedure which allows me, each time y drop
my treeview, to put the values of the attributs in a string whitch is
added
to the tag of the node .

Then, when I click on one node, with the split function, each element of
the
string (stored in the tag) are displayed in differents textboxes
corresponding
with each attributes of my treewiew.

But I am afraid of not using the more safe or appropriate function... In
this case
and if I have managed to be understood, may be know you some lines of code
which will allow me to reach my purpose ?

Thanks for your attention,

Evelyne

I'm not sure I follow. Do you have a procedure that does what you need
and just aren't sure if it's the best way? If so please post the code
and I'll be happy to look at it.

Thanks,

Seth Rowe

"""""
Thank you very much.

I have replied to you with a private message because I'm afraid that
my code won't be a good exemple for everybody...

Evelyne
 

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