Note that there are some explanatory texts on larger screens.

plurals
  1. POReading and modifying xmlNodes without affecting their children
    primarykey
    data
    text
    <p>I have a chunk of XML I would like to handle. I am writing this in C# but will use psuedocode to explain what I want to do. Please let me know if you need any more information. In C#, I load the XML into an XMLDocument as follows:</p> <pre><code>XmlDocument templateDoc = new XmlDocument(); templateDoc.LoadXml(selectionTemplate); XmlNodeList orderNodes = templateDoc.GetElementsByTagName("order"); </code></pre> <p>XML: </p> <pre><code>&lt;sort&gt; &lt;order name="panelCode"&gt; &lt;panelCode/&gt; &lt;/order&gt; &lt;order name="panelName"&gt; &lt;panelName/&gt; &lt;/order&gt; &lt;order name="panelPart"&gt; &lt;panelPart/&gt; &lt;/order&gt; &lt;order name="numberLayers"&gt;descending &lt;numberLayers/&gt; &lt;panelCode&gt;ascending|ascending &lt;/panelCode&gt; &lt;/order&gt; &lt;/sort&gt; </code></pre> <p>Psuedocode:</p> <pre><code>foreach ( orderNode ) { if ( order node title matches my string ) { if ( order node text is ascending or descending ) //This check Im struggling with as I only want to check the order node text and not include any child node text in the comparison. { switch ascending with descending and vice versa //If i set the inner text of the order node it will effect the text contained within its child nodes. } else //order node text is blank { set ordernode text to be ascending //Again its the child nodes that could cause an issue. } } else // order node title does not match string { if ( order node text is ascending or descending ) { remove order node text whilst maintaining the inner text of any child nodes. //Again its the child nodes that could cause an issue. } } } </code></pre> <p>As an example of how this would work... </p> <p>If my string was panelCode I would loop through all the order nodes. The first would be a match so I would set the value of that order node to "ascending". I would then loop through the rest of the order nodes looking for any of them that have a value of ascending/descending. This would be true of the numberLayers order node whose value I would like to set to "" but maintain the values of the child nodes e.g ascending|ascending. So the parsed document would look like the following. </p> <p>New XML: </p> <pre><code>&lt;sort&gt; &lt;order name="panelCode"&gt;ascending &lt;panelCode/&gt; &lt;/order&gt; &lt;order name="panelName"&gt; &lt;panelName/&gt; &lt;/order&gt; &lt;order name="panelPart"&gt; &lt;panelPart/&gt; &lt;/order&gt; &lt;order name="numberLayers"&gt; &lt;numberLayers/&gt; &lt;panelCode&gt;ascending|ascending &lt;/panelCode&gt; &lt;/order&gt; &lt;/sort&gt; </code></pre> <p>If the string I had were numberLayers the supplied XML would turn out as follows:</p> <pre><code>&lt;sort&gt; &lt;order name="panelCode"&gt; &lt;panelCode/&gt; &lt;/order&gt; &lt;order name="panelName"&gt; &lt;panelName/&gt; &lt;/order&gt; &lt;order name="panelPart"&gt; &lt;panelPart/&gt; &lt;/order&gt; &lt;order name="numberLayers"&gt;ascending &lt;numberLayers/&gt; &lt;panelCode&gt;ascending|ascending &lt;/panelCode&gt; &lt;/order&gt; &lt;/sort&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload