2013-07-22 61 views
0

我正在開發一個asp.net和c#的web服務,它顯示了密碼保護的XML文件的內容poinstreak.com。我的問題是,我似乎無法獲得驗證工作(我一直得到401狀態)。有關如何解決這個問題的任何建議?如何在c#中驗證xml?

protected void dataBind() 
    { 
     //Variables 
     string xmlfile = "http://www.pointstreak.com/xml/GET/schedule_11200.xml"; 
     string xslfile = "/StyleSheet/XSLT/xslForGETMatches.xsl"; 

     //Credentials 
     NetworkCredential myCred = new NetworkCredential("username", "password"); 
     CredentialCache myCache = new CredentialCache(); 
     myCache.Add(new Uri(xmlfile), "Basic", myCred); 

     //Set the credentials on the XmlUrlResolver object 
     XmlUrlResolver resolver = new XmlUrlResolver(); 
     resolver.Credentials = myCache; 

     //XSL Transformation 
     XslCompiledTransform xslt = new XslCompiledTransform(); 
     xslt.Load(xslfile, XsltSettings.Default, resolver); 
     StringBuilder outfile = new StringBuilder(); 
     XmlWriterSettings settings = xslt.OutputSettings.Clone(); 
     settings.CheckCharacters = false; 
     xslt.Transform(xmlfile, XmlWriter.Create(outfile, settings)); 
     xmlToHtmlOutput.Text = outfile.ToString(); //An ASP Label 
    } 
+0

XmlUrlResolver用於在XSLT中使用'document'函數或類似函數。您的XSLT是否屬於這種情況?您可以通過WebClient直接獲取Xml - 例如,請參閱http://stackoverflow.com/a/9946968/1073107,然後您可以將它加載到XmlDocument中並直接進行轉換。 – dash

回答

0

您可以使用HttpWebRequest來獲取xml的內容並對其應用操作。

WebRequest request = WebRequest.Create(url); 
NetworkCredential credential = new NetworkCredential(username, password); 

request.Credentials = credential; 
request.PreAuthenticate = true; 

WebResponse response = request.GetResponse(); 
Stream Answer = response.GetResponseStream(); 

StreamReader _Answer = new StreamReader(Answer); 
string content = _Answer.ReadToEnd(); //the string of the whole xml 
response.Close(); 
+0

它的工作。謝謝! – patrico0023

+0

很高興我可以幫助:) – sora0419