2016-10-19 3 views
0

으로 조회하는 방법 I의 DOCTYPE 값의 다수 포함 XML의 다음과 같은 유형이 있습니다XPathSelectElement

<InvalidDocTypes> 
    <DocType>DocType1</DocType> 
    <DocType>DocType2</DocType> 
</InvalidDocTypes> 

나는 다음과 같은 사용하여 특정 문서 유형에 대한 XML을 조회하려고 :

document.PriorDocumentType = "DocType1" 
var node = doc.XPathSelectElement("//InvalidDocTypes[DocType='" + document.PriorDocumentType + "']"); 

XML에 값이 없을 때 노드가 null 일 것으로 예상되지만 항상 null 만 표시됩니다. XML에 대해 Linq 쿼리를 사용하는 것이 더 낫겠습니까, 아니면 XPathSelectElement를 잘못 사용하고 있습니다. 어떤 도움을 주시면 감사하겠습니다. 감사합니다.

+0

실제 XML은 * 기본 네임 스페이스가 있습니까 * : 그것은 전체에게 InvalidDocTypes 존재하지 않는 요소 에서 DocType이 존재하고 널을 인쇄? ('xmlns = "whatever"'를 찾으십시오.) – har07

+0

아니요, 네임 스페이스가 없습니다. Damian이 나를 위해 일했던 것처럼 XElement.Load에서 XDocument.Load로 전환. – Darthchai

답변

1

코드를 테스트했는데 작동하는 것 같습니다. 아래 콘솔 응용 프로그램을 확인하십시오.

using System; 
using System.Xml.Linq; 
using System.Xml.XPath; 

namespace ConsoleApplication5 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var xml = @"<InvalidDocTypes> 
    <DocType>DocType1</DocType> 
    <DocType>DocType2</DocType> 
</InvalidDocTypes>"; 

      var documentType = "DocType1"; 

      var xmlDocument = XDocument.Parse(xml); 
      var node = xmlDocument.XPathSelectElement("//InvalidDocTypes[DocType='" + documentType + "']"); 
      Console.WriteLine(node); 
     } 
    } 
} 
+0

감사합니다. 완벽하게 작동했습니다. 나는 XElement.Load (xmlfile)를 사용하고 있었고 나를 위해 일하지 않았다. 일단 내가 XDocument.Load (xmlfile)로 변경하면 효과가있다. – Darthchai