0

Let's say, I've got a XmlNode:

<A>1</A>

How to remove a text from it, so I get:

<A />

instead of:

<A></A>

Below is a unit test which shows what I tried so far.

  [Test]
  public void RemoveXmlTextTest()
  {
     string xmlString = @"<B><A>1</A></B>";         
     XmlDocument doc = new XmlDocument();
     doc.LoadXml(xmlString);
     XmlNode testNode = doc.SelectSingleNode("//A");

     //1. Set innerText to string.Empty - not working
     //testNode.InnerText = String.Empty;

     //2. Call removeAll - not working         
     //testNode.RemoveAll();

     //3. testNode has one child with name '#text' - remove it - not working
     //testNode.RemoveChild(testNode.FirstChild);

     //4. Replace node with a new empty one - working but extremally ugly :(
     //All references to testNode still points to the old one !!!!
     testNode.ParentNode.ReplaceChild(doc.CreateElement(testNode.Name), testNode);

     //Is there some better way to do it?

     testNode = doc.SelectSingleNode("//A");
     Assert.AreEqual("<A />", testNode.OuterXml);
  }

1 Answer 1

2

Have you tried setting XmlElement.IsEmpty to true?

(Obviously this means casting to XmlElement, but that's the only situation in which the question makes sense.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.