2011-01-31 6 views
5

이렇게 보이는 csproj 파일에서 노드를 가져 오려고하지만 네임 스페이스 선언으로 인해 작동하지 않습니다.XElement를 사용하여 네임 스페이스의 노드를 쿼리합니다.

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build"> 
     <PropertyGroup> 
      <RegisterForComInterop>true</RegisterForComInterop> 

이 비참하게 실패

XDocument cpo = XDocument.Load(file); 
XmlNamespaceManager nsm = new XmlNamespaceManager(new NameTable()); 
nsm.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/200"); 
IEnumerable<XElement> list3 = cpo.XPathSelectElements("//x:RegisterForComInterop[.='true']", nsm); 

누구나 어떤 아이디어하세요?

감사합니다.

답변

12

정말로 XPath를 사용 하시겠습니까?

XDocument cpo = XDocument.Load(file); 
XNamespace x = "http://schemas.microsoft.com/developer/msbuild/2003"; 
var elements = cpo.Descendants(x + "RegisterForComInterop") 
        .Where(x => (string) x == "true"); 

또는 모든 RegisterForComInterop 적절한 부울 값이있을 것이라는 점을 절대적으로 확신이 있다면 당신은 explicit XElement to bool conversion 사용할 수 있습니다 : 개인적으로

XDocument cpo = XDocument.Load(file); 
XNamespace x = "http://schemas.microsoft.com/developer/msbuild/2003"; 
var elements = cpo.Descendants(x + "RegisterForComInterop") 
        .Where(x => (bool) x); 

을 나는 것 그것은 XML에 LINQ 내에서 네임 스페이스를 사용하는 정말 쉽습니다 네임 스페이스가 관련되어 있으면 일반적으로 XPath 보다는이 아닌이 경로로 이동하십시오.

+0

부울 문자열을 100 % 기대하기 때문에 마지막 줄을'.Where (x => bool.Parse (x.Value)) '로 바꿀 것입니다. – Shimmy

+0

그리고 배치 (batch) 퍼포먼스를 위해서이 선택은 오버 헤드가 적다. – Shimmy

+0

@Shimmy : 나는 bool을 사용하지 않을 것입니다. 여기서는 - XML에 대해 알고있는 'bool'에 대한 명시적인 변환을 사용합니다. 편집 할 것입니다. –

관련 문제