2009-04-16 6 views
0

의 말을하자, 나는있는 XmlNode있어 :XmlNode에서 텍스트를 제거하는 방법?

<A>1</A> 

이 어떻게에서 텍스트를 제거하기를, 그래서 내가 얻을 :

<A /> 

대신 :

다음
<A></A> 

입니다 지금까지 시도한 것을 보여주는 단위 테스트.

[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); 
    } 

답변

2

XmlElement.IsEmpty을 true로 설정 했습니까?

(당연히 이는 XmlElement으로 전송된다는 것을 의미하지만 질문이 의미있는 유일한 상황입니다.)

관련 문제