How to insert a node in xml using visual basic express ?

E

E.F

Hi everybody,

I get lost trying to insert a node in my xml file with
readfile, xpath, xpathnavigator, etc...

1/ my xml file looks 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" />
</YEAR_2007>
</PUPILL_CP_1>

2/ I want to insert this node
"<MARS EffectifDeLaClasse="28" Presents="10" Absents="18"
Comment="something" />"
afert the last (which is <FEVRIER EffectifDeLaClasse="28" Presents="27"
Absents="1" Comment="something else" />)

Does anybody can explain me the best way to select one nodes
while reading this file and after, the best way to insert my node ?

Thank you very much for your attention.

Evelyne
 
M

Martin Honnen

E.F said:
1/ my xml file looks like that :

<PUPILL_CP_1>
<YEAR_2006>
<OCTOBRE EffectifDeLaClasse="28" Presents="25" Absents="3" Comment=""

^^^^^
That tag is not closed so the sample is not well-formed XML.
<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" />
</YEAR_2007>
</PUPILL_CP_1>

2/ I want to insert this node
"<MARS EffectifDeLaClasse="28" Presents="10" Absents="18"
Comment="something" />"
afert the last (which is <FEVRIER EffectifDeLaClasse="28" Presents="27"
Absents="1" Comment="something else" />)

Use XmlDocument like this:

Dim XmlDoc As XmlDocument = New XmlDocument()
XmlDoc.Load("..\..\XMLFile1.xml")
Dim Mars As XmlElement = XmlDoc.CreateElement("Mars")
Mars.SetAttribute("EffectifDeLaClasse", "28")
Mars.SetAttribute("Presents", "10")
Mars.SetAttribute("Absents", "18")
Mars.SetAttribute("Comment", "something")

XmlDoc.SelectSingleNode("(PUPILL_CP_1/*)[last()]").AppendChild(Mars)
' Saving to console for testing
' save to file in real application
XmlDoc.Save(Console.Out)


If you don't want the create the element and its attribute by hand with
several calls but want to parse a string then use e.g.

Dim XmlDoc As XmlDocument = New XmlDocument()
XmlDoc.Load("..\..\XMLFile1.xml")
Dim Fragment As XmlDocumentFragment =
XmlDoc.CreateDocumentFragment()
Fragment.InnerXml = "<MARS EffectifDeLaClasse=""28""
Presents=""10"" Absents=""18"" Comment=""something"" />"

XmlDoc.SelectSingleNode("(PUPILL_CP_1/*)[last()]").AppendChild(Fragment)
XmlDoc.Save(Console.Out)
 
J

jayeldee

Does anybody can explain me the best way to select one nodes
while reading this file and after, the best way to insert my node ?

Thank you very much for your attention.

Evelyne

If you're not completely sold on using node/xml manipulation, I'd
recommend trying out serialization. I'm a lazy man and I find it a
lot easier to let the CLR handle all my reading/writing from XML and I
just pass it a class or expect a class back from it.
 
E

E.F

Martin Honnen said:
E.F said:
1/ my xml file looks like that :

<PUPILL_CP_1>
<YEAR_2006>
<OCTOBRE EffectifDeLaClasse="28" Presents="25" Absents="3"
Comment=""

^^^^^
That tag is not closed so the sample is not well-formed XML.
<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" />
</YEAR_2007>
</PUPILL_CP_1>

2/ I want to insert this node
"<MARS EffectifDeLaClasse="28" Presents="10" Absents="18"
Comment="something" />"
afert the last (which is <FEVRIER EffectifDeLaClasse="28" Presents="27"
Absents="1" Comment="something else" />)

Use XmlDocument like this:

Dim XmlDoc As XmlDocument = New XmlDocument()
XmlDoc.Load("..\..\XMLFile1.xml")
Dim Mars As XmlElement = XmlDoc.CreateElement("Mars")
Mars.SetAttribute("EffectifDeLaClasse", "28")
Mars.SetAttribute("Presents", "10")
Mars.SetAttribute("Absents", "18")
Mars.SetAttribute("Comment", "something")

XmlDoc.SelectSingleNode("(PUPILL_CP_1/*)[last()]").AppendChild(Mars)
' Saving to console for testing
' save to file in real application
XmlDoc.Save(Console.Out)


If you don't want the create the element and its attribute by hand with
several calls but want to parse a string then use e.g.

Dim XmlDoc As XmlDocument = New XmlDocument()
XmlDoc.Load("..\..\XMLFile1.xml")
Dim Fragment As XmlDocumentFragment =
XmlDoc.CreateDocumentFragment()
Fragment.InnerXml = "<MARS EffectifDeLaClasse=""28""
Presents=""10"" Absents=""18"" Comment=""something"" />"

XmlDoc.SelectSingleNode("(PUPILL_CP_1/*)[last()]").AppendChild(Fragment)
XmlDoc.Save(Console.Out)

Thank you very much for your help.

Evelyne
 
E

E.F

jayeldee said:
If you're not completely sold on using node/xml manipulation, I'd
recommend trying out serialization. I'm a lazy man and I find it a
lot easier to let the CLR handle all my reading/writing from XML and I
just pass it a class or expect a class back from it.

Thank you for this proposition. In fact, I need to present my file
in a treeview. This is the reason why I work with xml.

Is it possible to do such thing with serialisation ?
 
J

jayeldee

Thank you for this proposition. In fact, I need to present my file
in a treeview. This is the reason why I work with xml.

Is it possible to do such thing with serialisation ?

Sure is! Sent you an email with an example that was a bit big and I
didn't want to litter up the thread.
 
E

E.F

jayeldee said:
Sure is! Sent you an email with an example that was a bit big and I
didn't want to litter up the thread.


Hi !

I'm very interested with your example, but I didn't received it...

May I ask you where I can find it or may be could you try to send
it right to my mail one more time.

Best regards,

Evelyne
 
J

jayeldee

Hi !

I'm very interested with your example, but I didn't received it...

May I ask you where I can find it or may be could you try to send
it right to my mail one more time.

Best regards,

Evelyne

I sent it to the email in your profile but maybe it didn't go
through. I'm jayeldee-at-gmail-dot-com if you want to try sending me
something that I can reply to.
 
Top