获取 XslCompiledTransform.Load 以加载 SharePoint 列表中的文件

发布于 2024-08-23 18:12:43 字数 814 浏览 5 评论 0原文

我在使用 XslCompiledTransform.Load 方法获取服务器路径时遇到困难。我用谷歌搜索了一下,发现我需要做类似的事情:

xslt.Load(System.Web.HttpContext.Server.MapPath(xslPath),
           XsltSettings.Default, new XmlUrlResolver());

但它返回了一个错误,说 HttpContext is null。

我也尝试过:

xslt.Load(System.Web.HttpServerUtility.MapPath(xslPath), 
           XsltSettings.Default, new XmlUrlResolver());

这也返回了一个错误,指出非静态字段、方法或属性需要对象引用 System.Web.HttpServerUtility.MapPath(string)

xslPath 有一个指向的路径到 Sharepoint Web 中的 xsl 文件。我只想 XslCompiledTransform 加载带有服务器路径的 xsl 文件。是否可以?如果是这样,正确的方法或黑客的方法是什么?

编辑:我可以访问包含 xsl 文件路径的 SPWeb 对象。但是,当我检查 ServerRelativeUrl 时,它只是显示“/MyTree/xsl.xsl”。这里的问题是我无法让 XslCompiledTransform.Load 从 SharePoint 列表加载文件。

谢谢。

I'm having difficulties getting XslCompiledTransform.Load method to take a server path. I googled around and found that I need to do something like:

xslt.Load(System.Web.HttpContext.Server.MapPath(xslPath),
           XsltSettings.Default, new XmlUrlResolver());

But it returned an error saying HttpContext is null.

I also tried:

xslt.Load(System.Web.HttpServerUtility.MapPath(xslPath), 
           XsltSettings.Default, new XmlUrlResolver());

That also returned an error saying an object reference is required for the non-static field, method, or property System.Web.HttpServerUtility.MapPath(string)

The xslPath has a path that points to a xsl file in Sharepoint Web. I just want XslCompiledTransform to load the xsl file with the server path. Is it possible? If so, what is the proper way or hackish way of doing it?

EDIT: I have access to a SPWeb object which contains the path to the xsl file. However when I check the ServerRelativeUrl, it just says "/MyTree/xsl.xsl". The problem here is I couldn't get the XslCompiledTransform.Load to load the file from SharePoint list.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫雨熙 2024-08-30 18:12:43

在处理请求期间,当前的 HttpContext 是 HttpContext.Current。在页面/用户控件/WebPart 中,这也是属性Context

HttpContext.Context.Server.MapPath(xslPath)

如果在处理请求期间未调用您的方法,则 HttpContext.Current 将为 null。在这种情况下,您可以手动映射路径。

public string MapPath(string path)
{
    if (HttpContext.Current != null)
        return HttpContext.Current.Server.MapPath(path);

    path = path.Replace("/", @"\");
    if (path.StartsWith(@"~\")) {
        path = path.Substring( 2 );
    } else if (path.StartsWith(@"\")) {
        path = path.Substring( 1 );
    }
    // a non-prefixed path is already relative to your web server root

    return Path.Combine( HttpRuntime.AppDomainAppPath, path );
}

以上是一般情况下在 ASP.NET 中映射磁盘路径的情况。

如果该文件包含在您的 SPWeb 对象中,您应该使用 SPWeb.GetFile

SpWeb web;

SPFile file = web.GetFile( path );

XmlReader r = XmlReader.Create( file.OpenBinaryStream() );
xslt.Load( r );

During a the processing of a request, the current HttpContext is HttpContext.Current. In a Page/UserControl/WebPart this is also the property Context.

HttpContext.Context.Server.MapPath(xslPath)

If your method is not called during the processing of a request, HttpContext.Current will be null. In this case you could map the path manually.

public string MapPath(string path)
{
    if (HttpContext.Current != null)
        return HttpContext.Current.Server.MapPath(path);

    path = path.Replace("/", @"\");
    if (path.StartsWith(@"~\")) {
        path = path.Substring( 2 );
    } else if (path.StartsWith(@"\")) {
        path = path.Substring( 1 );
    }
    // a non-prefixed path is already relative to your web server root

    return Path.Combine( HttpRuntime.AppDomainAppPath, path );
}

The above is for mapping disk paths in ASP.NET in general.

If the file is contained in you SPWeb object, you should use SPWeb.GetFile

SpWeb web;

SPFile file = web.GetFile( path );

XmlReader r = XmlReader.Create( file.OpenBinaryStream() );
xslt.Load( r );
还如梦归 2024-08-30 18:12:43

我最终发现,如果我传入一个与本地路径不同的 url,XslCompiledTransform 类将自动切换到不同的模式以将路径读取为 URL。

如果我传入一个仅包含文件名的字符串,XslCompiledTransform 将在我的本地硬盘中查找该文件。同样,如果我传递类似 /myFolder/myXsl.xsl 的内容。

但是,如果我传入一个共享点 URL,例如 web.ParentWeb.Url + NameOfFile,那么它将从共享点 URL 中读取它。

我不是 100% 确定为什么它会自动切换,但至少上述对我有用。

What I found in the end is that if I pass in a url that does not resemble a local path, the XslCompiledTransform class will automatically switch to a different mode to read the path as a URL.

If I pass in a string that contains only the name of the file, XslCompiledTransform will look for the file in my local hard disk. Likewise if I pass in something like /myFolder/myXsl.xsl.

However, if I pass in a sharepoint URL e.g. web.ParentWeb.Url + NameOfFile, then it'll go off to read it from the Sharepoint URL.

I'm not 100% sure why it does the automatic switch, but at least the above worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文