Change existing element in XML file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yhlove@gmail.com

    #1

    Change existing element in XML file

    Hi

    I'm trying to change element in xml file as described below:

    the xml file:
    ----------------

    <MyProg>
    <Path>test1</Path>
    </MyProg>

    I want to change the element Path to test2 instead of test1
    How can I do that ? Is there a function which gets full xml path and
    new value and change the element in this path?

    Thanks
    yhlove

  • rowe_newsgroups

    #2
    Re: Change existing element in XML file

    On May 7, 10:29 am, yhl...@gmail.co m wrote:
    Hi
    >
    I'm trying to change element in xml file as described below:
    >
    the xml file:
    ----------------
    >
    <MyProg>
    <Path>test1</Path>
    </MyProg>
    >
    I want to change the element Path to test2 instead of test1
    How can I do that ? Is there a function which gets full xml path and
    new value and change the element in this path?
    >
    Thanks
    yhlove
    I believe you need to store the element into an XmlNode object and
    then change it's innertext property. You can "grab" the node by using
    an XmlDocument's GetElementByTag Name method.

    Thanks,

    Seth Rowe

    Comment

    • Martin Honnen

      #3
      Re: Change existing element in XML file

      yhlove@gmail.co m wrote:
      <MyProg>
      <Path>test1</Path>
      </MyProg>
      >
      I want to change the element Path to test2 instead of test1
      How can I do that ? Is there a function which gets full xml path and
      new value and change the element in this path?
      Dim XmlDoc As XmlDocument = New XmlDocument()
      XmlDoc.Load("fi le.xml")
      XmlDoc.Document Element("Path") .InnerText = "test2"
      XmlDoc.Save("fi le.xml")

      In general to find nodes you can also use XPath and the methods
      SelectNodes and SelectSingleNod e.

      --

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

      Comment

      • yhlove@gmail.com

        #4
        Re: Change existing element in XML file

        On May 7, 5:46 pm, rowe_newsgroups <rowe_em...@yah oo.comwrote:
        On May 7, 10:29 am, yhl...@gmail.co m wrote:
        >
        >
        >
        Hi
        >
        I'm trying to change element in xml file as described below:
        >
        the xml file:
        ----------------
        >
        <MyProg>
        <Path>test1</Path>
        </MyProg>
        >
        I want to change the element Path to test2 instead of test1
        How can I do that ? Is there a function which gets full xml path and
        new value and change the element in this path?
        >
        Thanks
        yhlove
        >
        I believe you need to store the element into an XmlNode object and
        then change it's innertext property. You can "grab" the node by using
        an XmlDocument's GetElementByTag Name method.
        >
        Thanks,
        >
        Seth Rowe
        Hi

        Could u please write me example code.
        How can store XmlNode to the xml file ? which function to that?

        Thanks

        Comment

        • yhlove@gmail.com

          #5
          Re: Change existing element in XML file

          On May 7, 5:59 pm, Martin Honnen <mahotr...@yaho o.dewrote:
          yhl...@gmail.co m wrote:
          <MyProg>
          <Path>test1</Path>
          </MyProg>
          >
          I want to change the element Path to test2 instead of test1
          How can I do that ? Is there a function which gets full xml path and
          new value and change the element in this path?
          >
          Dim XmlDoc As XmlDocument = New XmlDocument()
          XmlDoc.Load("fi le.xml")
          XmlDoc.Document Element("Path") .InnerText = "test2"
          XmlDoc.Save("fi le.xml")
          >
          In general to find nodes you can also use XPath and the methods
          SelectNodes and SelectSingleNod e.
          >
          --
          >
          Martin Honnen --- MVP XML
          http://JavaScript.FAQTs.com/
          Hi

          It's work! Thanks a lot.

          Another problem:
          if the xml is look like that:
          <MyFile>
          <elem1>
          <elem2>test1</elem2>
          </elem1>
          </MyFile>

          and I want to change the element elem2 which is son of elem1. How can
          I do that?
          I tried to do it in that way but it didn't work:
          XmlDoc.Document Element("elem2" ).InnerText = "test2"
          or
          XmlDoc.Document Element("elem1/elem2").InnerTe xt = "test2"

          Thanks

          Comment

          • Martin Honnen

            #6
            Re: Change existing element in XML file

            yhlove@gmail.co m wrote:
            if the xml is look like that:
            <MyFile>
            <elem1>
            <elem2>test1</elem2>
            </elem1>
            </MyFile>
            >
            and I want to change the element elem2 which is son of elem1. How can
            I do that?
            I tried to do it in that way but it didn't work:
            XmlDoc.Document Element("elem2" ).InnerText = "test2"
            You can only find child nodes with the index syntax so you would need
            XmlDoc.Document Element("elem1" )("elem2").Inne rText = "test2"
            or, if you want to use XPath expressions then you need to use
            SelectSingleNod e
            XmlDoc.Document Element("elem1/elem2").InnerTe xt = "test2"
            XmlDoc.Document Element.SelectS ingleNode("elem 1/elem2").InnerTe xt =
            "test2"


            --

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

            Comment

            Working...