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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • E.F

    #1

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

    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 EffectifDeLaCla sse="28" Presents="25" Absents="3" Comment=""
    <NOVEMBRE EffectifDeLaCla sse="28" Presents="28" Absents="0"
    Comment="...." />
    <DECEMBRE EffectifDeLaCla sse="28" Presents="20" Absents="5"
    Comment="blabla bla" />
    </YEAR_2006>
    <YEAR_2007>
    <JANVIER EffectifDeLaCla sse="28" Presents="10" Absents="18"
    Comment="DANGER important desease"/>
    <FEVRIER EffectifDeLaCla sse="28" Presents="27" Absents="1"
    Comment="someth ing else" />
    </YEAR_2007>
    </PUPILL_CP_1>

    2/ I want to insert this node
    "<MARS EffectifDeLaCla sse="28" Presents="10" Absents="18"
    Comment="someth ing" />"
    afert the last (which is <FEVRIER EffectifDeLaCla sse="28" Presents="27"
    Absents="1" Comment="someth ing 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


  • Martin Honnen

    #2
    Re: How to insert a node in xml using visual basic express ?

    E.F wrote:
    1/ my xml file looks like that :
    >
    <PUPILL_CP_1>
    <YEAR_2006>
    <OCTOBRE EffectifDeLaCla sse="28" Presents="25" Absents="3" Comment=""
    ^^^^^
    That tag is not closed so the sample is not well-formed XML.
    <NOVEMBRE EffectifDeLaCla sse="28" Presents="28" Absents="0"
    Comment="...." />
    <DECEMBRE EffectifDeLaCla sse="28" Presents="20" Absents="5"
    Comment="blabla bla" />
    </YEAR_2006>
    <YEAR_2007>
    <JANVIER EffectifDeLaCla sse="28" Presents="10" Absents="18"
    Comment="DANGER important desease"/>
    <FEVRIER EffectifDeLaCla sse="28" Presents="27" Absents="1"
    Comment="someth ing else" />
    </YEAR_2007>
    </PUPILL_CP_1>
    >
    2/ I want to insert this node
    "<MARS EffectifDeLaCla sse="28" Presents="10" Absents="18"
    Comment="someth ing" />"
    afert the last (which is <FEVRIER EffectifDeLaCla sse="28" Presents="27"
    Absents="1" Comment="someth ing else" />)
    Use XmlDocument like this:

    Dim XmlDoc As XmlDocument = New XmlDocument()
    XmlDoc.Load(".. \..\XMLFile1.xm l")
    Dim Mars As XmlElement = XmlDoc.CreateEl ement("Mars")
    Mars.SetAttribu te("EffectifDeL aClasse", "28")
    Mars.SetAttribu te("Presents", "10")
    Mars.SetAttribu te("Absents", "18")
    Mars.SetAttribu te("Comment", "something" )

    XmlDoc.SelectSi ngleNode("(PUPI LL_CP_1/*)[last()]").AppendChild( Mars)
    ' Saving to console for testing
    ' save to file in real application
    XmlDoc.Save(Con sole.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.xm l")
    Dim Fragment As XmlDocumentFrag ment =
    XmlDoc.CreateDo cumentFragment( )
    Fragment.InnerX ml = "<MARS EffectifDeLaCla sse=""28""
    Presents=""10"" Absents=""18"" Comment=""somet hing"" />"

    XmlDoc.SelectSi ngleNode("(PUPI LL_CP_1/*)[last()]").AppendChild( Fragment)
    XmlDoc.Save(Con sole.Out)


    --

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

    Comment

    • jayeldee

      #3
      Re: How to insert a node in xml using visual basic express ?

      On Mar 13, 12:39 am, "E.F" <fenouillet...@ laposte.netwrot e:
      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.



      Comment

      • E.F

        #4
        Re: How to insert a node in xml using visual basic express ?


        "Martin Honnen" <mahotrash@yaho o.dea écrit dans le message de news:
        ufj1SEXZHHA.392 8@TK2MSFTNGP03. phx.gbl...
        E.F wrote:
        >
        >1/ my xml file looks like that :
        >>
        ><PUPILL_CP_1 >
        > <YEAR_2006>
        > <OCTOBRE EffectifDeLaCla sse="28" Presents="25" Absents="3"
        >Comment=""
        >
        ^^^^^
        That tag is not closed so the sample is not well-formed XML.
        >
        > <NOVEMBRE EffectifDeLaCla sse="28" Presents="28" Absents="0"
        >Comment="... ." />
        > <DECEMBRE EffectifDeLaCla sse="28" Presents="20" Absents="5"
        >Comment="blabl a bla" />
        > </YEAR_2006>
        > <YEAR_2007>
        > <JANVIER EffectifDeLaCla sse="28" Presents="10" Absents="18"
        >Comment="DANGE R important desease"/>
        > <FEVRIER EffectifDeLaCla sse="28" Presents="27" Absents="1"
        >Comment="somet hing else" />
        > </YEAR_2007>
        ></PUPILL_CP_1>
        >>
        >2/ I want to insert this node
        >"<MARS EffectifDeLaCla sse="28" Presents="10" Absents="18"
        >Comment="somet hing" />"
        >afert the last (which is <FEVRIER EffectifDeLaCla sse="28" Presents="27"
        >Absents="1" Comment="someth ing else" />)
        >
        Use XmlDocument like this:
        >
        Dim XmlDoc As XmlDocument = New XmlDocument()
        XmlDoc.Load(".. \..\XMLFile1.xm l")
        Dim Mars As XmlElement = XmlDoc.CreateEl ement("Mars")
        Mars.SetAttribu te("EffectifDeL aClasse", "28")
        Mars.SetAttribu te("Presents", "10")
        Mars.SetAttribu te("Absents", "18")
        Mars.SetAttribu te("Comment", "something" )
        >
        XmlDoc.SelectSi ngleNode("(PUPI LL_CP_1/*)[last()]").AppendChild( Mars)
        ' Saving to console for testing
        ' save to file in real application
        XmlDoc.Save(Con sole.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.xm l")
        Dim Fragment As XmlDocumentFrag ment =
        XmlDoc.CreateDo cumentFragment( )
        Fragment.InnerX ml = "<MARS EffectifDeLaCla sse=""28""
        Presents=""10"" Absents=""18"" Comment=""somet hing"" />"
        >
        XmlDoc.SelectSi ngleNode("(PUPI LL_CP_1/*)[last()]").AppendChild( Fragment)
        XmlDoc.Save(Con sole.Out)
        >
        >
        --
        >
        Martin Honnen --- MVP XML
        http://JavaScript.FAQTs.com/
        Thank you very much for your help.

        Evelyne


        Comment

        • E.F

          #5
          Re: How to insert a node in xml using visual basic express ?


          "jayeldee" <jayeldee@gmail .coma écrit dans le message de news:
          1173800069.8332 83.160860@64g20 00...legro ups.com...
          On Mar 13, 12:39 am, "E.F" <fenouillet...@ laposte.netwrot e:
          >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 ?



          Comment

          • jayeldee

            #6
            Re: How to insert a node in xml using visual basic express ?

            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.

            Comment

            • E.F

              #7
              Re: How to insert a node in xml using visual basic express ?

              "jayeldee" <jayeldee@gmail .coma écrit dans le message de news:
              1173880587.1255 37.238170@n59g2 00...legr oups.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


              Comment

              • jayeldee

                #8
                Re: How to insert a node in xml using visual basic express ?

                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.

                Comment

                Working...