2014-05-20 5 views
0

에서 노드를 제거 I이하여 XDocument하여 XDocument

<axes dimension="y"> 
    <axis id="y11" scale="log" label="label1"> 
     ... 
    </axis> 
    <axis id="y12" scale="log" label="label1"> 
     ... 
    </axis> 
    </axes> 
    <axes dimension="x"> 
    <axis id="x0" label=""> 
     ... 
    </axis> 
    <axis id="x1" label=""> 
     ... 
    </axis> 
    </axes> 

이가하여 XDocument에 있고 나는 그것을에서 Y12 축을 제거하고 남은 나머지를 마칠에서 다음 XML 조각. 따라서, 최종 출력은

<axes dimension="y"> 
    <axis id="y11" scale="log" label="label1"> 
     ... 
    </axis> 
    </axes> 
    <axes dimension="x"> 
    <axis id="x0" label=""> 
     ... 
    </axis> 
    <axis id="x1" label=""> 
     ... 
    </axis> 
    </axes> 

는이 작업을 수행 할 수있는 방법이 될 것입니다?

나는이 시도했지만, 당신이하여 XDocument 아닌 XElement를 작업하고 있기 때문에이

xDocument 
    .Elements("axes") 
    .Where(x => (string)x.Attribute("dimension") == "y") 
    .Elements("axis") 
    .Where(x => (string)x.Attribute("id") == "y12") 
    .Remove(); 

답변

0

작동하지 않습니다, 당신은에 Elements 방법을 얻기 위해 Root 속성을 사용한다 루트 떨어져 요소를 의도 한대로 작동하고 찾을 :

대신 xDocument.Elements("axes")... 사용의 :

xDocument.Root.Elements("axes") 
    .Where(x => (string)x.Attribute("dimension") == "y") 
    .Elements("axis") 
    .Where(x => (string)x.Attribute("id") == "y12") 
    .Remove(); 

또는, 스키 수 직접 Descendants를 사용하여 페이지 Root :

xDocument.Descendants("axes") 
    .Where(x => (string)x.Attribute("dimension") == "y") 
    .Elements("axis") 
    .Where(x => (string)x.Attribute("id") == "y12") 
    .Remove(); 
0

이 시도 :

xDocument.Descendants("axis") 
    .Where(x => (string)x.Attribute("id") == "y12") 
    .Remove();