1

This is the code:-

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 

  }    

I am getting error that

Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\XmldataList.xml'.

At line-

  XDocument document =  XDocument.Load("XmldataList.xml");
1
  • Well does that file exist, and does your program have access to it? Check the path that the error message specifies. Additionally, please put more effort into formatting your code when you post. Commented Jul 22, 2014 at 5:47

2 Answers 2

1

You should try to specify the absolute path to XmldataList.xml:

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

Also, it could be helpful to start your IDE with administrator privileges.

Sign up to request clarification or add additional context in comments.

Comments

1

The best way to do that is using MapPath function as below :

String fullPath=MapPath("/XmldataList.xml");
XDocument.Load(fullPath);

Otherwise when you push the website on the live server your path might be broken. If still have problem check out the output of MapPath function, if it is correct, it might be security access issue and you should give IIS the full access to your folder. Always be careful about file name, sometimes your file name is slightly different with the name in your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.