tags:

views:

38

answers:

2

I have an xml as below

< Image>ImageValue11
<Type>png</Type>
<Value>ImageValue11</ Value>
</ Image>

Here ImageValue1 is present in two places.I want to remove innerText for Image node which is the parent.For which i am usign below code

XmlNode customImageNode = imagedoc.SelectSingleNode("//Image");
customImageNode.InnerText = string.empty;

But this is clearing the child nodes as well.Please let me know how to clear this test off .Looking for a generic solution.

+3  A: 

I believe what you need to do is select just the text node of the Image node. You do this like this:

XmlNode customImageNodeTextNode = imageDoc.SelectSingleNode("//Image/text()");
customImageNodeTextNode.InnerText = string.Empty;

I just tested the above code and it worked for me. Good luck!

Dan Tao
thanks Dan,It works .solved a lot of problem
Ravisha
A: 

try doing: customImageNode.Value= string.empty;

update: sorry for the mistake. It's supposed to be like so: customImageNode.RemoveChild(customImageNode.FirstChild);, since the inner text of the node is it's first child in this case. I've checked it and it works.

RA
that does not point to innerText of the Node
Ravisha
you're absolutely right, I've updated the answer.
RA
I think Dan's answer looks cleaner.Anyway thanks a lot for the help RA
Ravisha