【问题标题】:how to add XML file to a sharepoint project如何将 XML 文件添加到共享点项目
【发布时间】:2012-01-05 15:59:48
【问题描述】:

我正在开发一个应该读取 XML 文件的webpart。为此,我创建了一个类,其中 NodeValue() 方法应返回节点内的文本。如果使用控制台应用程序进行测试,这确实有效,但是当我将此(阅读器类和自定义 XML 文件)添加到我的 SharePoint 项目并尝试部署它时,我收到错误消息 Web Part error: File Not Found。我尝试更改文件属性,以便将其添加到编译版本,但我似乎仍然无法指定文件的路径。这是阅读器类:

using System;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OrgChartPart.WebPartCode
{
class ReadXml
{

    public string FilePath { get; set; }

    public ReadXml()
    {
        FilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)+"\\LAYOUTS\\config.xml";
    }

    public string NodeValue(int pos)
    {
        if (pos <= 0)
            return "invalid position!(this is not a node value)";

        XmlTextReader textReader = new XmlTextReader(FilePath);

        int counter = 0;

        while (counter != pos)
        {
            textReader.Read();
            if (textReader.NodeType.ToString() == "Text")
                counter++;
        }
        return textReader.Value;
    }
}
}

这是我在其中实例化此类以读取文件的 Web 部件代码的一部分:

StringBuilder sb = new System.Text.StringBuilder();

.......
            string position = null, link_type = null, link_color = null, 
                node_alignment = null, node_fill = null, node_color_style = null, hyperlinks = null;

            ReadXml readXml = new ReadXml();

            sb.Append(@"
........

我不知道我是否在识别文件路径时遇到问题,还是需要提高权限才能访问它?或者,当解决方案部署时,目录结构发生了变化,所以我指定了错误的路径?你怎么看? 提前致谢

【问题讨论】:

    标签: xml sharepoint web-parts


    【解决方案1】:

    SharePoint 无法在指定路径找到文件绝对是一个问题。您可以将文件上传到文档库并从那里读取。

    我的猜测是 - 在上述情况下,如果您的 dll 是基于 bin 的,代码将尝试从您的虚拟目录的 bin 文件夹生成物理路径。

    另外 - SPContext.Current.Web.Url +“_layouts”+“模板下的文件夹”+“配置文件名”也应该有效。

    HTH, 尼丁·拉斯托吉

    【讨论】:

      【解决方案2】:

      如果您想保存设置来管理您的 Web 部件,您可以通过这种方式来提供它。您可以将设置放在 web.config 中并以与标准 ASP.NET 应用程序相同的方式访问它们。但是我认为这不是一个好主意,我认为最好使用 SharePoint API 来提供 Web 部件设置。 (幸运的是,设置启用 Web 部件非常容易。)。 It’s good example 使用 SharePoint API

      【讨论】:

        【解决方案3】:

        好吧,我设法解决了它..有点.. 我通过 SharePoint API(在农场)添加了 XML 文件(或 CSS 或任何你想要的),之后我能够使用文件的虚拟路径访问文件(你可以使用 Sharepoint Design Manager 和查看目录结构打开你的农场),

        【讨论】:

          猜你喜欢
          • 2015-01-23
          • 2017-03-29
          • 2011-06-13
          • 1970-01-01
          • 1970-01-01
          • 2019-12-16
          • 1970-01-01
          • 2010-11-25
          • 1970-01-01
          相关资源
          最近更新 更多