Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. linqtoxml-update

linqtoxml-update

Scheduled Pinned Locked Moved LINQ
csharplinqquestionannouncement
12 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arkiboys
    wrote on last edited by
    #1

    <menu1> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>2</mk> </m> </kp> </field4> ... </menu1> <menu2> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field4> ... </menu2> Hi, Using linq how is it possible to do the following please? Thanks in <menu1>, find <mv>54321</mv> <mk>2</mk> and replace it with the values of: <mk>1</mk> which is under <mv>12345</mv> therefore the result for <menu1> is: <menu1> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field4> ... </menu1> <menu2> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field4> ... </menu2>

    L M 2 Replies Last reply
    0
    • A arkiboys

      <menu1> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>2</mk> </m> </kp> </field4> ... </menu1> <menu2> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field4> ... </menu2> Hi, Using linq how is it possible to do the following please? Thanks in <menu1>, find <mv>54321</mv> <mk>2</mk> and replace it with the values of: <mk>1</mk> which is under <mv>12345</mv> therefore the result for <menu1> is: <menu1> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field4> ... </menu1> <menu2> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field4> ... </menu2>

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      It may well help your case, to format your sample xml using proper indents and placing between <pre></pre> tags to make it more readable.

      A 1 Reply Last reply
      0
      • L Lost User

        It may well help your case, to format your sample xml using proper indents and placing between <pre></pre> tags to make it more readable.

        A Offline
        A Offline
        arkiboys
        wrote on last edited by
        #3

        how do I do this? thanks

        L 1 Reply Last reply
        0
        • A arkiboys

          how do I do this? thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          arkiboys wrote:

          how do I do this?

          Are you serious? You don't know how to type these tags, <pre></pre>, around a block of code? Or even press the "code block" button at the top of the edit screen?

          1 Reply Last reply
          0
          • A arkiboys

            <menu1> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>2</mk> </m> </kp> </field4> ... </menu1> <menu2> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field4> ... </menu2> Hi, Using linq how is it possible to do the following please? Thanks in <menu1>, find <mv>54321</mv> <mk>2</mk> and replace it with the values of: <mk>1</mk> which is under <mv>12345</mv> therefore the result for <menu1> is: <menu1> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field4> ... </menu1> <menu2> <field1> <value1></value1> <value2></value2> </field1> <field2> <kp> <m> <mv>12345</mv> <mk>1</mk> </m> </kp> </field2> <field3> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field3> <field4> <kp> <m> <mv>54321</mv> <mk>1</mk> </m> </kp> </field4> ... </menu2>

            M Offline
            M Offline
            Mirko1980
            wrote on last edited by
            #5

            I have some free time, so here you are:

            var sourceQuery = from mNode in doc.Root.Descendants("m")
            let mvNode = mNode.Element("mv")
            let mkNode = mNode.Element("mk")
            where (mvNode.Value == "54321") &&
            (mkNode.Value == "2")
            select mkNode;
            var sourceNode = sourceQuery.First();

            var targetQuery = from mNode in doc.Root.Descendants("m")
            let mvNode = mNode.Element("mv")
            let mkNode = mNode.Element("mk")
            where (mvNode.Value == "12345")
            select mkNode;
            var targetNode = targetQuery.First();

            sourceNode.ReplaceWith(targetNode);

            I'm sure you will be able to fully understand the syntax and logic behind it. If not, go here and do some studying before asking the same question over and over. On an additional note, I used <pre> tags to make my code readable, as you can see.

            A 1 Reply Last reply
            0
            • M Mirko1980

              I have some free time, so here you are:

              var sourceQuery = from mNode in doc.Root.Descendants("m")
              let mvNode = mNode.Element("mv")
              let mkNode = mNode.Element("mk")
              where (mvNode.Value == "54321") &&
              (mkNode.Value == "2")
              select mkNode;
              var sourceNode = sourceQuery.First();

              var targetQuery = from mNode in doc.Root.Descendants("m")
              let mvNode = mNode.Element("mv")
              let mkNode = mNode.Element("mk")
              where (mvNode.Value == "12345")
              select mkNode;
              var targetNode = targetQuery.First();

              sourceNode.ReplaceWith(targetNode);

              I'm sure you will be able to fully understand the syntax and logic behind it. If not, go here and do some studying before asking the same question over and over. On an additional note, I used <pre> tags to make my code readable, as you can see.

              A Offline
              A Offline
              arkiboys
              wrote on last edited by
              #6

              Hi, I see what you are trying to do. thanks What i am puzzled about is that what if you are only interested to look at the data and not anything else i.e. you do not what to refer to data. How can the code be altered to cater for this? And I believe .Root should not be in the code? Thank you

              M 1 Reply Last reply
              0
              • A arkiboys

                Hi, I see what you are trying to do. thanks What i am puzzled about is that what if you are only interested to look at the data and not anything else i.e. you do not what to refer to data. How can the code be altered to cater for this? And I believe .Root should not be in the code? Thank you

                M Offline
                M Offline
                Mirko1980
                wrote on last edited by
                #7

                I really don't understand what you mean. Can you try to explain better? As for Root, yes, maybe is not really needed.

                A 1 Reply Last reply
                0
                • M Mirko1980

                  I really don't understand what you mean. Can you try to explain better? As for Root, yes, maybe is not really needed.

                  A Offline
                  A Offline
                  arkiboys
                  wrote on last edited by
                  #8

                  Ok, let's say the xml is as follows and then look at the result ast the bottom. I am only interested to work in <Accounts> and not <positions> or anything else. The value 8 for mapKey of 123456 is replaced by 3 because 3 is the mapvalue of the 6543 mapKey Hope you see what I mean. Thank you XML --- <config> <column1> <value>something</value> </column1> ... <Accounts> <ownermap> <keymap> <Mapping> <mapKey>123456</mapKey> <mapValue>8</mapValue> </Mapping> <Mapping> <mapKey>6543</mapKey> <mapValue>3</mapValue> </Mapping> <Mapping> <Mapping> <mapKey>123456</mapKey> <mapValue>7</mapValue> </Mapping> <Mapping> ... </keymap> </ownermap> </Accounts> <positions> <ownermap> <keymap> <Mapping> <mapKey>123456</mapKey> <mapValue>8</mapValue> </Mapping> <Mapping> <mapKey>76566</mapKey> <mapValue>7</mapValue> </Mapping> <Mapping> ... </keymap> </ownermap> </positions> ... </config> ---- RESULT ---- <config> <column1> <value>something</value> </column1> ... <Accounts> <ownermap> <keymap> <Mapping> <mapKey>123456</mapKey> <mapValue>3</mapValue> </Mapping> <Mapping> <mapKey>6543</mapKey> <mapValue>3</mapValue> </Mapping> <Mapping> <Mapping> <mapKey>123456</mapKey> <mapValue>7</mapValue> </Mapping> <Mapping> ... </keymap> </ownermap> </Accounts> <positions> <ownermap> <keymap> <Mapping> <mapKey>123456</mapKey> <mapValue>8</mapValue> </Mapping> <Mapp

                  M 1 Reply Last reply
                  0
                  • A arkiboys

                    Ok, let's say the xml is as follows and then look at the result ast the bottom. I am only interested to work in <Accounts> and not <positions> or anything else. The value 8 for mapKey of 123456 is replaced by 3 because 3 is the mapvalue of the 6543 mapKey Hope you see what I mean. Thank you XML --- <config> <column1> <value>something</value> </column1> ... <Accounts> <ownermap> <keymap> <Mapping> <mapKey>123456</mapKey> <mapValue>8</mapValue> </Mapping> <Mapping> <mapKey>6543</mapKey> <mapValue>3</mapValue> </Mapping> <Mapping> <Mapping> <mapKey>123456</mapKey> <mapValue>7</mapValue> </Mapping> <Mapping> ... </keymap> </ownermap> </Accounts> <positions> <ownermap> <keymap> <Mapping> <mapKey>123456</mapKey> <mapValue>8</mapValue> </Mapping> <Mapping> <mapKey>76566</mapKey> <mapValue>7</mapValue> </Mapping> <Mapping> ... </keymap> </ownermap> </positions> ... </config> ---- RESULT ---- <config> <column1> <value>something</value> </column1> ... <Accounts> <ownermap> <keymap> <Mapping> <mapKey>123456</mapKey> <mapValue>3</mapValue> </Mapping> <Mapping> <mapKey>6543</mapKey> <mapValue>3</mapValue> </Mapping> <Mapping> <Mapping> <mapKey>123456</mapKey> <mapValue>7</mapValue> </Mapping> <Mapping> ... </keymap> </ownermap> </Accounts> <positions> <ownermap> <keymap> <Mapping> <mapKey>123456</mapKey> <mapValue>8</mapValue> </Mapping> <Mapp

                    M Offline
                    M Offline
                    Mirko1980
                    wrote on last edited by
                    #9

                    So, you need to operate only on descendants of the Accounts node. Do you remember I used Root in my example? that was the search starting point. You can go on specifying subnodes at will, such as doc.Root.Elements("Accounts") Seriously, read the link I gave you and try to understand it before asking further questions. And use the code block when inserting XML, you have been already told to do so many times.

                    A 1 Reply Last reply
                    0
                    • M Mirko1980

                      So, you need to operate only on descendants of the Accounts node. Do you remember I used Root in my example? that was the search starting point. You can go on specifying subnodes at will, such as doc.Root.Elements("Accounts") Seriously, read the link I gave you and try to understand it before asking further questions. And use the code block when inserting XML, you have been already told to do so many times.

                      A Offline
                      A Offline
                      arkiboys
                      wrote on last edited by
                      #10

                      Ok, There is no .Root under doc var doc = XElement.Load(FILE_PATH); var sourceQuery = from mNode in doc.Descendants("m") let mvNode = mNode.Element("mv") let mkNode = mNode.Element("mk") where (mvNode.Value == "54321") && (mkNode.Value == "2") select mkNode; Thanks

                      M 1 Reply Last reply
                      0
                      • A arkiboys

                        Ok, There is no .Root under doc var doc = XElement.Load(FILE_PATH); var sourceQuery = from mNode in doc.Descendants("m") let mvNode = mNode.Element("mv") let mkNode = mNode.Element("mk") where (mvNode.Value == "54321") && (mkNode.Value == "2") select mkNode; Thanks

                        M Offline
                        M Offline
                        Mirko1980
                        wrote on last edited by
                        #11

                        Use XDocument instead of XElement.

                        A 1 Reply Last reply
                        0
                        • M Mirko1980

                          Use XDocument instead of XElement.

                          A Offline
                          A Offline
                          arkiboys
                          wrote on last edited by
                          #12

                          Thank you

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups