private void button1_Click(object sender, EventArgs e)
        {
            string url = "../../URLConfig.xml";
            XmlTextReader xmlReader = new XmlTextReader(url);
            while (xmlReader.Read())
            {
                //判断是否循环到MainMenu节点
                if (!xmlReader.IsEmptyElement && xmlReader.Name == "MainMenu")
                {
                    //获取属性ID值
                    string id = xmlReader.GetAttribute("id");
                }
            }
        }-------------------------------------------------------------------------------
上面代码中 url  本项目能用,拷到别的地方,发布成服务就麻烦了,服务起来就挂了,进程直接崩溃查了下,估计路径路径有问题。
特此请教 如果才能百分百的 写对路径,不至于重蹈覆辙
XmlTextReader(URL)加载后的路径是怎么找的?

解决方案 »

  1.   

    如果你的要加载的文档和应用程序在同一目录,直接写文件名即可。 "../../URLConfig.xml"
    这里退2级,debug-->bin-->程序集目录(也就是项目中程序集下的文件)或者:
    System.Environment.CurrentDirectory+"/URLConfig.xml"
    Application.StartupPath()+"/URLConfig.xml"等。
    C# 获取当前路径方法
      

  2.   

    winform还是asp.net?winform的话最好用Application.StartupPath()+"/URLConfig.xml"来构造,如果是asp.net,应该用HttpServerUtility.MapPath("/URLConfig.xml"),如FilePath = Server.MapPath("/URLConfig.xml");
      

  3.   

    这是我的xml类,代码在App_Code文件夹下    public int Read( string item)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/XMLFile.xml"));
            XmlNodeList xNodeList = xDoc.GetElementsByTagName(“XXX”);
            return Convert.ToInt32(xNodeList[0].InnerXml);
        }
      

  4.   

    你发布后 ,如果你的目录层次增加或减少都是 "../../" 已经出现变化System.Environment.CurrentDirectory
    Application.StartupPath() 这2个 可以 不过是 文件在 EXE 的同目录或是 更深的目录,如果你飞的放在上一级目录。可以推荐你用反射,你吧你的 文件加上 namespace 然后 可以通过 Assembly 来找到这个文件的路径 最后 加载 读取。