1

我有一个在开发环境中工作的功能,但我需要更改路径,以便它在主机服务器上正确解析。

此代码行;

doc.Load("H:\Website_Sep2012\OtherDataFiles\dataXML.xml") 'this needs to be changed to the server path

在这个函数内

Public Shared Function GetList(ByVal nodestring As String) As List(Of String)
    Dim doc As New XmlDocument()

    'Load XML from the file into XmlDocument object
    doc.Load("H:\Website_Sep2012\OtherDataFiles\dataXML.xml") 'this needs to be changed to the server path
    Dim root As XmlNode = doc.DocumentElement

    'Select all nodes with the tag paramter indicated by the nodestring variable
    Dim nodeList As XmlNodeList = root.SelectNodes(nodestring)
    Return (From node As XmlNode In nodeList Select node.InnerText).ToList()
End Function

将字符串替换为Server.MapPath("~/OtherDataFiles\dataXML.xml")不起作用,因为 Server 在该范围内不可用。如何解决此路径的任何想法

4

2 回答 2

3

你也可以试试:

System.Web.Hosting.HostingEnvironment.MapPath()不需要HttpContext

或者

using System.Web;

HttpContext.Current.Server.MapPath("~/OtherDataFiles/dataXML.xml");
于 2013-02-21T13:49:29.133 回答
2

始终可以像这样访问服务器:

string filePath = System.Web.HttpContext.Current.Server.MapPath("~/OtherDataFiles/dataXML.xml");
doc.Load(filePath);

如果在类库项目中,您只需添加对 System.Web 程序集的引用。

于 2013-02-21T13:44:26.957 回答