Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 无法使用C分析Xml#_Asp.net_Xml_Xml Parsing - Fatal编程技术网

Asp.net 无法使用C分析Xml#

Asp.net 无法使用C分析Xml#,asp.net,xml,xml-parsing,Asp.net,Xml,Xml Parsing,代码如下:- public void TestXmlDocument() { StringBuilder output = new StringBuilder(); XDocument document = XDocument.Load("XmldataList.xml"); #region Fetch All the Books var books = from r in document.Descendants("book"

代码如下:-

public void TestXmlDocument()
  {
     StringBuilder output = new StringBuilder();
     XDocument document =  XDocument.Load("XmldataList.xml");

         #region Fetch All the Books 

         var books = from r in document.Descendants("book")
          select new 
              { 
                 Author = r.Element("author").Value,
                 Title = r.Element("title").Value,
                 Genere = r.Element("genre").Value,
                 Price = r.Element("price").Value,
                 PublishDate = r.Element("publish_date").Value,
                 Description = r.Element("description").Value,
              };
            foreach (var r in books)
            {
               Label3.Text=r.PublishDate + r.Title + r.Author;
            }
            Console.ReadKey(true);

            #endregion 

  }    
我得到一个错误

异常详细信息:System.IO.FileNotFoundException:找不到 文件'C:\Program Files\Common Files\Microsoft 共享\DevServer\10.0\XmldataList.xml'

排队-

  XDocument document =  XDocument.Load("XmldataList.xml");

您应该尝试指定XmldataList.xml的绝对路径:

XDocument document = XDocument.Load(@"C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\XmldataList.xml");

此外,使用管理员权限启动IDE也会很有帮助。

最好的方法是使用MapPath函数,如下所示:

String fullPath=MapPath("/XmldataList.xml");
XDocument.Load(fullPath);
否则,当您在live server上推送网站时,您的路径可能会中断。 如果仍然有问题,请检查MapPath函数的输出,如果正确,则可能是安全访问问题,您应该授予IIS对文件夹的完全访问权限。
请始终注意文件名,有时您的文件名与代码中的名称略有不同。

该文件是否存在,以及您的程序是否有权访问该文件?检查错误消息指定的路径。此外,请在发帖时花更多的精力格式化代码。