2010-03-17 4 views
10

나는이처럼 보이는하여 XDocument 있습니다는 닫는 태그를 포함하는

XDocument outputDocument = new XDocument(
       new XElement("Document", 
        new XElement("Stuff") 
       ) 
      ); 

그건 내가 이것에

outputDocument.ToString() 

출력을 호출 할 때 :

<Document> 
    <Stuff /> 
</Document> 

그러나 나는 그것이 같이 할 :

<Document> 
    <Stuff> 
    </Stuff> 
</Document> 

나는 첫 번째 것이 옳다는 것을 알지만,이 방법으로 출력해야합니다. 어떤 제안?

답변

12

각 빈 XElementValue 속성을 빈 문자열로 설정하십시오.

// Note: This will mutate the specified document. 
    private static void ForceTags(XDocument document) 
    { 
     foreach (XElement childElement in 
      from x in document.DescendantNodes().OfType<XElement>() 
      where x.IsEmpty 
      select x) 
     { 
      childElement.Value = string.Empty; 
     } 
    } 
+0

고마워요! XDocument 선언에 String.Empty를 넣으려고 할 때 올바른 경로에 있었지만 명백하게 무시되었습니다. –

+0

어떻게 역 변환을 수행 할 수 있습니까? 즉 대신 을보고 싶습니다. – Kamyar

관련 문제