locked
Load xml from a relative path RRS feed

  • Question

  • Hi guys, I would read an xml document with XDocument but I'm not able to read it because this exception is fired System.IO.DirectoryNotFoundException

    the tree structure of my program is this one

    - test.cs is under the directory org/mycompany, inside this class there is the Xdocument object

    XDocument xmlLoaded = XDocument.Load(relativepath);

    - the xml file (test.xml) is under the directory org/mycompany/documents

    I've tried with this one, but nothing. Alaways the same exception.

     XDocument xmlLoaded = XDocument.Load(@"./document/test.xml");

    How can I fix this problem? Thanks in advance

     

     

    Wednesday, March 31, 2010 11:30 AM

Answers

  • If the xml is in your Web project folder, then  you will need to use the WebClient method

     

    WebClient wc = new WebClient();
    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
    wc.DownloadStringAsync(new Uri("test.xml", UriKind.Relative));
    
    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show("Unable to load file");
            return;
        }
    
        XElement root = XElement.Parse(e.Result);
    }
     
    Wednesday, March 31, 2010 12:32 PM

All replies

  • The Load method is pointing to the "document" folder (not documents).

    XDocument xmlLoaded = XDocument.Load(@"./documents/test.xml");
     

    Is the xml hosted in the xap or the Web project?

    Wednesday, March 31, 2010 12:00 PM
  • I'm sorry, I have made a mistake...

    This is the error:

    Could not find a part of the path C:\Program Files\Microsoft Visual Studio 9\Common7\IDE\....\test.xml

    the xml is hosted in the Web project

    Wednesday, March 31, 2010 12:11 PM
  • If the xml is in your Web project folder, then  you will need to use the WebClient method

     

    WebClient wc = new WebClient();
    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
    wc.DownloadStringAsync(new Uri("test.xml", UriKind.Relative));
    
    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show("Unable to load file");
            return;
        }
    
        XElement root = XElement.Parse(e.Result);
    }
     
    Wednesday, March 31, 2010 12:32 PM
  • If the xml is in your Web project folder, then  you will need to use the WebClient method  WHY?

    But if test.xml is under an other directory (like the structure you have seen in first post), how can solve this problem?

    Thank you so much!!!

    Wednesday, March 31, 2010 12:40 PM
  • If the xml is not hosted in the xap file, then you can use the WebClient to load the xml to your application.  You can host the xml in the xap and use the direct relative path.

    Wednesday, March 31, 2010 1:18 PM
  • I've found the solution!!!

    If you want to load a file inside the clientbin directory (that directory generated under the web application), use webclient class.

    P.S. You have to set as default project the web project and not the silverlight one.

    If you want to read a file which is inside a web project (which a class that is inside the same web project) use this simple trick. For example, if you want to read an xml file through LINQ:

    XDocument xmlLoaded =
                    XDocument.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "relative_path_to_test/test.xml")

    Bye bye

    Friday, April 2, 2010 4:20 AM