2014-12-15 2 views
3

내 첫 번째 질문 ...XComment (C#을) 여기

내가 (하여 XDocument으로 C#을 사용) XML 파일을 구문 분석하고 일부 XElement를 객체를 해제하기 위해 노력하고있어에 XElement를 변환하는 방법. 표준 방법 (내가 일하는 곳)은 xComment로 나타나게하는 것입니다.

텍스트 파일로 구문 분석하는 것 외에는이를 수행 할 수있는 방법이 없습니다. 당신이 요구 한대로

<EnabledElement>ABC</EnabledElement> 
<!-- DisabledElement></DisabledElement--> 

답변

4

음이 아주을 아니지만, 이것은 주석 버전 요소를 교체하지 :

결과는 다음과 같이한다

using System; 
using System.Xml.Linq; 

public class Test 
{ 
    static void Main() 
    { 
     var doc = new XDocument(
      new XElement("root", 
       new XElement("value1", "This is a value"), 
       new XElement("value2", "This is another value"))); 

     Console.WriteLine(doc); 

     XElement value2 = doc.Root.Element("value2"); 
     value2.ReplaceWith(new XComment(value2.ToString())); 
     Console.WriteLine(doc); 
    } 
} 

출력 :

<root> 
    <value1>This is a value</value1> 
    <value2>This is another value</value2> 
</root> 

<root> 
    <value1>This is a value</value1> 
    <!--<value2>This is another value</value2>--> 
</root> 

value2.ReplaceWith(new XComment(value2.ToString().Trim('<', '>'))); 

을 ...하지만 나는 개인적 않을 것 : eally이 주석 열고 <>을 닫으면 요소에서 사람을 대체하기를 원하는, 당신은 사용할 수 있습니다.

+0

"ReplaceWith (...)"가 트릭을 완벽하게 수행했습니다. 감사합니다. –