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