2012-01-13 2 views
1

C#에서 사용자 지정 WebDAV 서버를 작성하고 있습니다. 내가 사용하고있는 클라이언트 테스트 프로그램 중 하나는 NetDrive이며 WebDAV를 준수하는 것으로 보입니다.C#에서 webdav 서버 XML 요청을 처리하는 방법

<?xml version="1.0" encoding="utf-8"?> 
<propfind xmlns="DAV:"> 
    <allprop/> 
</propfind> 

그러나 다른 클라이언트가이 작업을 수행 : 내 문제는 다음과 같은 형식으로 서버에 요청을 수신하고있다

두 개의 다른 네임 스페이스의 형식을 찾아 내 논리를 fooing 계속
<?xml version="1.0" encoding="utf-8"?> 
<D:propfind xmlns:D="DAV:"> 
    <D:allprop/> 
</D:propfind> 

"allprop"요소. 이제

string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><propfind xmlns=\"DAV:\"><allprop/></propfind>"; //Hardcode to make all the StackOverflow users' lives easier 
XPathDocument doc = new XPathDocument(new StringReader(xml)); 
XPathNavigator nav = doc.CreateNavigator(); 
XPathNodeIterator it = nav.Select("/propfind/*"); 

, 나는 내가 "DAV :"에 대한 네임 스페이스 매니저의 몇 가지 유형에 둘 필요가 알고 : 내 코드는 다음과 같은 비트 보이는, 그래서 나는이 시도 :

XmlNamespaceManager nsman = new XmlNamespaceManager(nav.NameTable); 
nsman.AddNamespace("", "DAV"); 
XPathNodeIterator it = nav.Select("/propfind/*", nsman); 

하지만를 첫 번째 XML 파일에 대한 반복기에서 노드를 얻지 못했습니다. 기본 네임 스페이스가 제대로 작동하지 않는 것 같습니다.

내가 뭘 잘못하고 있니? 네임 스페이스가 기본이 될 수 있거나 명시 적으로 명명 될 때이 XML에 allprop 노드가 있는지 쿼리하려면 어떻게합니까?

답변

0

네임 스페이스 URI (DAV :)를 찾고 그것이 존재하지 않으면 추가했습니다. 그런 다음 방금의 네임 스페이스를 입력하고 모든 테스트 케이스에서 작동했습니다.

XPathDocument document = new XPathDocument(xml); 
XPathNavigator navigator = document.CreateNavigator(); 

//Get namespaces & add them to the search 
bool hasDAV = false; 
string davPrefix = "D"; 
XmlNamespaceManager nsman = new XmlNamespaceManager(navigator.NameTable); 

foreach (KeyValuePair<string, string> nskvp in navigator.GetNamespacesInScope(XmlNamespaceScope.All)) 
{ 
    if (string.Compare(nskvp.Value, "DAV:", StringComparison.InvariantCultureIgnoreCase) == 0) 
    { 
     hasDAV = true; 
     davPrefix = nskvp.Key; 
    } 
    nsman.AddNamespace(nskvp.Key, nskvp.Value); 
} 

if (!hasDAV) 
    nsman.AddNamespace(davPrefix , "DAV:"); 


XPathNodeIterator iterator = navigator.Select("/" + davPrefix + ":" + WebDavXML.PropFind + "/*", nsman); 
1

코드에서 잘못된 네임 스페이스를 사용하고 있습니다. Unforunalely WebDAV 사양은 WebDAV 노드 및 특성의 네임 스페이스로 'DAV :'를 사용합니다 (이는 의 XML 네임 스페이스 메커니즘에 대한 오해로 인한 것 같습니다).

관련 문제