How to correctly use XmlNode.ReplaceChild() to change the Elements subtree?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aredz
    New Member
    • Aug 2010
    • 6

    How to correctly use XmlNode.ReplaceChild() to change the Elements subtree?

    Hi,

    I have this XMLData. (SAMPLE ONLY)

    XML1:

    <text>This is a text
    <i>great</i>
    <ref>call this sample</ref>
    Another text
    <b>This is noted</b>
    Underlying text
    </text>

    XML2:

    <reference><r ef attr="8">Rare</ref><text>hello </text></reference>

    ------------

    This block of code: (SAMPLE)
    Code:
    foreach (XmlNode xmlData in xmlTextLst)
    {
    
    if (xmlData.HasChildNodes)
    {
    for (int i = 0; i < xmlData.ChildNodes.Count; i++)
    {
    if (xmlData.ChildNodes[i].Name == "ref")
    {
    xmlData.ReplaceChild(xmlTempRoot, xmlData.ChildNodes[i]);
    }
    }
    }
    
    szTextList.Add(Convert.ToString(xmlData.InnerXml));
    
    }
    Appending the value of szTextList to a <XmlTextNode>
    Code:
    txtNode[iterator].AppendChild((XmlText)szTextList[iterator]);
    Although I can process the output, the output doesn't seem to be what I wanted it to be.

    Here is the sample output:

    &lt;text&gt;Thi s is a text
    &lt;i&gt;great& lt;/i&gt;
    &lt;reference&g t;&lt;ref attr="8"&gt;Rar e&lt;/ref&gt;&lt;text &gt;hello&lt ;/text&gt;&lt;/reference&gt;
    Another text
    &lt;b&gt;Thi s is noted&lt;/b&gt;
    Underlying text
    &lt;text&gt;

    which should be like this:

    <text>This is a text
    <i>great</i>
    <reference><r ef attr="8">Rare</ref><text>hello </text></reference>
    Another text
    <b>This is noted</b>
    Underlying text
    </text>

    And as I can see it is printing like a simple text not as nodes.

    Although I can see how it ended like this (it is because it is a appended as a XmlText and not an XmlElement), I can't seem to find a way that it will retain it's nodes Value and Types as well.

    Can anyone please advise about this?

    Thanks,

    aredz~
  • aredz
    New Member
    • Aug 2010
    • 6

    #2
    Hi,

    I have fixed this.

    using this line of code:

    Code:
    XmlCurrentNode.InnerXml = XmlExistingNode.InnerXml;
    Kindly close this thread.

    aredz~

    Comment

    Working...