XML access to a child innertext
Question
I have this XML:
<location>
<character name="name" mask="pap.png" X="397" Y="60">
<look reaction="False">
text1
<answer>text2</answer>
</look>
<talk xml=""/>
</character>
</location>
and I have the access to the node . I have some problem in changing the text1
and the text2
. Here my code:
node.ChildNodes[0].InnerText = "new_text1";
node.ChildNodes[0].ChildNodes[0].InnerText = "new_text2"; //this doesn't work
after that my XML looks like:
<location>
<character name="name" mask="pap.png" X="397" Y="60">
<look reaction="False">text1</look>
<talk xml=""/>
</character>
</location>
so it cut off the <answer/>
child. Any ideas?
Solution
You must change InnerXml
. because of your XML is Mixed Complex Type (Complex Type=mixed : xml element can contain attributes, elements, and text) and DOM is not aware completely for this ones. your code can be like this:
node.ChildNodes[0].InnerXml = "new_text1 <answer>new_text2</answer>";
OTHER TIPS
If this is the line node.ChildNodes[0].InnerText = "new_text1";
that replace
<look reaction="False">
text1
<answer>text2</answer>
</look>
then you have already replace the tag <answer>text2</answer>
from it
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow