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. Other Discussions
  3. The Weird and The Wonderful
  4. Lack of understanding about the XmlDocument (C#)

Lack of understanding about the XmlDocument (C#)

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharprubyxmljsonquestion
8 Posts 6 Posters 0 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.
  • D Offline
    D Offline
    DrWheetos
    wrote on last edited by
    #1

    I spotted this gem written by one of our developers that needed to copy an XML node from one XML document (source) into another (target). The variable naming's been changed to make it clearer. Given 2 XML nodes:

    XmlNode sourceNode = source.SelectSingleNode("BOOKS/BOOK[@id='1234']");
    XmlNode targetNode = target.SelectSingleNode("COMPANY/BOOKS");

    This is the code the developer wrote.

    XmlElement bookElement = target.CreateElement("BOOK");
    targetNode = targetNode.AppendChild(bookElement);
    targetNode.InnerXml = sourceNode.InnerXml;

    How performant do you think it would be compared to this?

    XmlNode importedNode = target.ImportNode(sourceNode, true);
    targetNode.AppendChild(importedNode);

    I haven't bothered to do the tests but I guess stringifying some XML and then parsing it back again would be a tad inefficient!

    P P P U 4 Replies Last reply
    0
    • D DrWheetos

      I spotted this gem written by one of our developers that needed to copy an XML node from one XML document (source) into another (target). The variable naming's been changed to make it clearer. Given 2 XML nodes:

      XmlNode sourceNode = source.SelectSingleNode("BOOKS/BOOK[@id='1234']");
      XmlNode targetNode = target.SelectSingleNode("COMPANY/BOOKS");

      This is the code the developer wrote.

      XmlElement bookElement = target.CreateElement("BOOK");
      targetNode = targetNode.AppendChild(bookElement);
      targetNode.InnerXml = sourceNode.InnerXml;

      How performant do you think it would be compared to this?

      XmlNode importedNode = target.ImportNode(sourceNode, true);
      targetNode.AppendChild(importedNode);

      I haven't bothered to do the tests but I guess stringifying some XML and then parsing it back again would be a tad inefficient!

      P Offline
      P Offline
      Perspx
      wrote on last edited by
      #2

      Yes, I assume so. As once the node is created it's stored in memory ready to be attached to another node, whereas if the XML is "stringified" then I assume the text is parsed, the node generated and copied to memory and then attached. The other obvious advantage is less code :cool: Regards, --Perspx

      "The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript

      1 Reply Last reply
      0
      • D DrWheetos

        I spotted this gem written by one of our developers that needed to copy an XML node from one XML document (source) into another (target). The variable naming's been changed to make it clearer. Given 2 XML nodes:

        XmlNode sourceNode = source.SelectSingleNode("BOOKS/BOOK[@id='1234']");
        XmlNode targetNode = target.SelectSingleNode("COMPANY/BOOKS");

        This is the code the developer wrote.

        XmlElement bookElement = target.CreateElement("BOOK");
        targetNode = targetNode.AppendChild(bookElement);
        targetNode.InnerXml = sourceNode.InnerXml;

        How performant do you think it would be compared to this?

        XmlNode importedNode = target.ImportNode(sourceNode, true);
        targetNode.AppendChild(importedNode);

        I haven't bothered to do the tests but I guess stringifying some XML and then parsing it back again would be a tad inefficient!

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        At least he was trying to use Xml. I'm sick and tired of seeing questions where people are looking for nodes in an Xml document and just treating the whole thing as one big string.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        L 1 Reply Last reply
        0
        • P Pete OHanlon

          At least he was trying to use Xml. I'm sick and tired of seeing questions where people are looking for nodes in an Xml document and just treating the whole thing as one big string.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          At least we CAN blame VB for this one!

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          D 1 Reply Last reply
          0
          • L leppie

            At least we CAN blame VB for this one!

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 alpha 4a out now (29 May 2008)

            D Offline
            D Offline
            DrWheetos
            wrote on last edited by
            #5

            Yes, I remember in VB6 days when you had an MSXML2 and MSXML4 documents, stringifying the XML was the only way to copy data across from one to another. The MSXML parser didn't like you mixing different versions.

            P 1 Reply Last reply
            0
            • D DrWheetos

              Yes, I remember in VB6 days when you had an MSXML2 and MSXML4 documents, stringifying the XML was the only way to copy data across from one to another. The MSXML parser didn't like you mixing different versions.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              DrWheetos wrote:

              Yes, I remember in VB6 days when you had an MSXML2 and MSXML4 documents

              Never, ever admit to remembering VB6 days. Claim you had amnesia or something instead. You'll only get tarred with a brush you don't want.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              1 Reply Last reply
              0
              • D DrWheetos

                I spotted this gem written by one of our developers that needed to copy an XML node from one XML document (source) into another (target). The variable naming's been changed to make it clearer. Given 2 XML nodes:

                XmlNode sourceNode = source.SelectSingleNode("BOOKS/BOOK[@id='1234']");
                XmlNode targetNode = target.SelectSingleNode("COMPANY/BOOKS");

                This is the code the developer wrote.

                XmlElement bookElement = target.CreateElement("BOOK");
                targetNode = targetNode.AppendChild(bookElement);
                targetNode.InnerXml = sourceNode.InnerXml;

                How performant do you think it would be compared to this?

                XmlNode importedNode = target.ImportNode(sourceNode, true);
                targetNode.AppendChild(importedNode);

                I haven't bothered to do the tests but I guess stringifying some XML and then parsing it back again would be a tad inefficient!

                P Offline
                P Offline
                Paul Conrad
                wrote on last edited by
                #7

                Yes, it would be a bit on the inefficient side. :rolleyes:

                "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

                1 Reply Last reply
                0
                • D DrWheetos

                  I spotted this gem written by one of our developers that needed to copy an XML node from one XML document (source) into another (target). The variable naming's been changed to make it clearer. Given 2 XML nodes:

                  XmlNode sourceNode = source.SelectSingleNode("BOOKS/BOOK[@id='1234']");
                  XmlNode targetNode = target.SelectSingleNode("COMPANY/BOOKS");

                  This is the code the developer wrote.

                  XmlElement bookElement = target.CreateElement("BOOK");
                  targetNode = targetNode.AppendChild(bookElement);
                  targetNode.InnerXml = sourceNode.InnerXml;

                  How performant do you think it would be compared to this?

                  XmlNode importedNode = target.ImportNode(sourceNode, true);
                  targetNode.AppendChild(importedNode);

                  I haven't bothered to do the tests but I guess stringifying some XML and then parsing it back again would be a tad inefficient!

                  U Offline
                  U Offline
                  User 3634035
                  wrote on last edited by
                  #8

                  Yes! buddy :-\

                  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