2017-09-07 1 views
2

나는 아주 간단한 작업을하고 있습니다. 작은 XML 파일에서 노드를 검색하려고합니다.XML 노드 선택

<?xml version="1.0" encoding="UTF-8" ?> 
<SAMLConfiguration xmlns="urn:componentspace:SAML:2.0:configuration"> 
    <IdentityProvider Name="IdpNameInSPForIsuer" 
        Description="SecureAuth" 
        LocalCertificateFile="" 
        LocalCertificatePassword=""/> 

    <ServiceProviderProfiles> 
    <ServiceProvider NameIdentifier ="SPIssuerName" 
        ExpectsSignatureVerification="true" 
        ExpectsSignedResponse="false" 
        Certificate="sharedpubliccsert.cer" 
        DigestMethod="SAMLIdentifiers.DigestMethods.SHA1" 
        SignatureMethod="SAMLIdentifiers.SignatureMethods.RSA_SHA1" 
        SingleLogoutServiceUrl="https://serviceprovider/slo" 
         SendResponseBy="HTTP-Redirect" /> 

    </ServiceProviderProfiles> 
</SAMLConfiguration> 

ServiceProvider를 얻으려고합니다. 아래에있는 C# 코드는 다음과 같습니다.

string parent = "ServiceProviderProfiles"; string children = "ServiceProvider";

var nodePath = string.Concat (@ "//", parent, @ "/", children);
var xmlNode = xmlDocument.SelectSingleNode (nodePath);

디버깅 할 때 xmlNode는 null입니다. 내 코드로 인해 xmlNode가 null이되는 문제는 무엇입니까?

+0

이다 "componentspace : SAML : 2.0 : 항아리를 구성"[C#에서 기본 네임 스페이스로 XPath는 사용]과 같이' (https://stackoverflow.com/a/585822/3744182) 및 [XmlDocument.SelectSingleNode 및 xmlNamespace 문제] (https://stackoverflow.com/a/4171468) 및 [SelectSingleNode는 XPath를 사용하여 알려진 양호한 XML 노드 경로에 대해 null을 반환합니다. ] (https://stackoverflow.com/q/1089196/3744182). – dbc

+0

[C#에서 기본 네임 스페이스로 Xpath 사용하기] (https://stackoverflow.com/a/585822/3744182)를 사용하여 [fiddle] (https://dotnetfiddle.net/py0mfe) 샘플을 다운로드하십시오. – dbc

답변

1

이렇게하는 방법에는 여러 가지가 있습니다. 여기

당신은 기본 네임 스페이스`의 xmlns = 사용하여 검색을 사용할 필요가 샘플 코드

// xmlns attribute from the root 
XNamespace ns = "urn:componentspace:SAML:2.0:configuration"; 
// read XML file into XmlDocument 
XDocument doc = XDocument.Load("file.xml"); 
// Select XML descendants with Linq 
var result = doc.Descendants(ns + "SAMLConfiguration").Descendants().Where(c => c.Name.LocalName.ToString() == "ServiceProvider") 
      .ToArray(); 
+0

대답이 업데이트됩니다. – Tcraft

+0

좋아요! 업데이트 해 주셔서 감사합니다. 해피 코딩! –