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. XML / XSL
  4. How do I move XML nodes?

How do I move XML nodes?

Scheduled Pinned Locked Moved XML / XSL
csharpquestionasp-netdata-structuresxml
2 Posts 2 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.
  • L Offline
    L Offline
    lnong
    wrote on last edited by
    #1

    I am working on an ASP.NET application using C#. In my XMLDocument, I have a node (that contains other nodes within) that I want to move to another location in the tree. How do move that node AND everything it contains? Is there a MoveNode() method? All I see is a ReplaceChild() method. Will that do the job? The only way I could think of using the ReplaceChild() method is the create a new XMLNode at the new desired location, and then replacing that node with the old one. Then use RemoveChild() to delete the old node. Any better ways? Thanks.

    Richard DeemingR 1 Reply Last reply
    0
    • L lnong

      I am working on an ASP.NET application using C#. In my XMLDocument, I have a node (that contains other nodes within) that I want to move to another location in the tree. How do move that node AND everything it contains? Is there a MoveNode() method? All I see is a ReplaceChild() method. Will that do the job? The only way I could think of using the ReplaceChild() method is the create a new XMLNode at the new desired location, and then replacing that node with the old one. Then use RemoveChild() to delete the old node. Any better ways? Thanks.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Try something like:

      XmlNode MoveNode(XmlNode nodeToMove, XmlNode newParent)
      {
      // Check we have some values:
      if (null == nodeToMove) throw new ArgumentNullException("nodeToMove");
      if (null == newParent) throw new ArgumentNullException("newParent");

      // Are we moving within the same document?
      if (nodeToMove.OwnerDocument == newParent.OwnerDocument)
      {
          // Check that the new parent is not a
          // child of the node we are moving:
          XmlNode temp = newParent;
          while(null != temp)
          {
              if (temp == nodeToMove) 
                  throw new InvalidOperationException();
              
              temp = temp.ParentNode;
          }
          
          // Remove the node from its old parent:
          if (null != nodeToMove.ParentNode)
              nodeToMove.ParentNode.RemoveChild(nodeToMove);
          
          // Add the node to the new parent:
          return newParent.AppendChild(nodeToMove);
      }
      else
      {
          // Import the node to the new document
          XmlNode newNode = newParent.OwnerDocument.ImportNode(nodeToMove, true);
          
          // Remove the node from its old parent:
          if (null != nodeToMove.ParentNode)
              nodeToMove.ParentNode.RemoveChild(nodeToMove);
          
          // Add the imported node to the new parent:
          return newParent.AppendChild(newNode);
      }
      

      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      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