Replace Xml Node with Raw Xml in .Net

Posted by OmegaMan at August 21, 2007

Category: How To, XML


In this post I will simulate a user editing a raw XmlNode from an existing xml document. Say for example we extract the node to edit, with subnodes, and place that in a text box. The user edits the text adding whatever is needed, but keeping with the original nodes, and then signals they are done. Upon that signal we take that raw fragment from the textbox and insert it into the document. Here is the code:

   1: string xmlInitial =
   2: @"<?xml version='1.0'?>
   3: <Rules>
   4:     <OpenBalances function='ReOrderFifo'>
   5:         <column name='SecurityID' used='True'/>
   6:         <column name='COL2' used='False'>#@#</column>
   7:         <column name='COL3' used='False'>#@#</column>
   8:     </OpenBalances>
   9:     <ClosedBalances/>
  10: </Rules>";
  12: string xmlCreatedByTheUser =
  13: @"<OpenBalances function='ReOrderFifo' iAmNew='true'>
  14:     <column name='SecurityID' used='True'/>
  15:     <column name='COL2' used='False'>#@#</column>
  16:     <column name='COL3' used='False'>#@#</column>
  17: </OpenBalances>";
  19:     XmlDocument originalXml = new XmlDocument();
  21:     string targetNode = "descendant::*[name(.) ='OpenBalances']";
  23:     originalXml.LoadXml(xmlInitial);
  25:     // Simulate the selection of the subnode
  26:     // for the user to edit in the first nodes
  27:     // Rules.
  28:     XmlNode editNode = originalXml.SelectSingleNode(targetNode);
  30:     // Get a fragment and slide the changed data into it.
  31:     XmlDocumentFragment fragment = originalXml.CreateDocumentFragment();
  32:     fragment.InnerXml = xmlCreatedByTheUser;
  34:     // Replace the contents of the editNode with the user fragment.
  35:     editNode.ParentNode.ReplaceChild(fragment, editNode);
  37:     Console.WriteLine(originalXml.OuterXml);
  • Line 01: This is our original Xml which we will use.
  • Line 12: This is the simulated change by the user. The user adds one attribute iAmNew.
  • Line 21: We will use this Xpath to extract the node to work on for the user.
  • Line 23: We load the initial Xml into the document.
  • Line 28: Simulated extraction of the node to display to the user.
  • Line 31: Its important that we create a Xml fragment from our original XmlDocument. We could not prune another XmlDocument, or create a fragment on the fly, it must come from the original Xml.
  • Line 32: Simulated user change and loading from a TextBox.
  • Line 35: Do the replacement here.
  • Line 37: Output the Xml.

Console Output:

   1: <?xml version="1.0"?>
   2: <Rules>
   3:    <OpenBalances function="ReOrderFifo" iAmNew="true">
   4:        <column name="SecurityID" used="True" />
   5:        <column name="COL2" used="False">##@##</column>
   6:        <column name="COL3" used="False">##@##</column>
   7:    </OpenBalances>
   8:    <ClosedBalances />
   9: </Rules>

Leave a comment

(required) (will not be published)

This site uses Akismet to reduce spam. Learn how your comment data is processed.