468,720 Members | 1,911 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

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

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
Mar 13 '07 #1
7 1299
E.F wrote:
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)
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 13 '07 #2
On Mar 13, 12:39 am, "E.F" <fenouillet...@laposte.netwrote:
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.

Mar 13 '07 #3
E.F

"Martin Honnen" <ma*******@yahoo.dea écrit dans le message de news:
uf**************@TK2MSFTNGP03.phx.gbl...
E.F wrote:
>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)
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Thank you very much for your help.

Evelyne
Mar 14 '07 #4
E.F

"jayeldee" <ja******@gmail.coma écrit dans le message de news:
11**********************@64g2000cwx.googlegroups.c om...
On Mar 13, 12:39 am, "E.F" <fenouillet...@laposte.netwrote:
>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.
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 ?

Mar 14 '07 #5
On Mar 14, 6:39 am, "E.F" <fenouillet...@laposte.etwrote:
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.

Mar 14 '07 #6
E.F
"jayeldee" <ja******@gmail.coma écrit dans le message de news:
11**********************@n59g2000hsh.googlegroups. com...
On Mar 14, 6:39 am, "E.F" <fenouillet...@laposte.etwrote:
>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.

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
Mar 15 '07 #7
On Mar 14, 9:28 pm, "E.F" <fenouillet...@laposte.etwrote:
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.

Mar 15 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bootstrap Bill | last post: by
26 posts views Thread by Bruno Jouhier [MVP] | last post: by
2 posts views Thread by Progman | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.