none
Reading XML File - Getting WebException Error

    Question

  • I have an xml file that I am trying to read when a certain page loads.  After the InitializeComponent() function, I call this function:

    private void LoadUnitInformation()
            {
                WebClient wClient = new WebClient();
                wClient.OpenReadCompleted += new OpenReadCompletedEventHandler(wClient_OpenReadCompleted);
                wClient.OpenReadAsync(new Uri("Assets/xml/units.xml", UriKind.Relative));
            }
    
            void wClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
            {
                if (e.Error == null && e.Result != null)
                {
                    XDocument xDoc = XDocument.Parse(e.Result.ToString(), LoadOptions.None);
                    List<Listing> UnitListings = (from listing in xDoc.Elements("Listings")
                                                  select new Listing()
                                                  {
                                                      UnitNum = (string)listing.Element("UnitNum"),
                                                      UnitType = (string)listing.Element("UnitType"),
                                                      Image = (string)listing.Element("Image"),
                                                      Description = (string)listing.Element("Description"),
                                                      RatePeriod1 = (string)listing.Element("RatePeriod1"),
                                                      RatePeriod2 = (string)listing.Element("RatePeriod2"),
                                                      RatePeriod3 = (string)listing.Element("RatePeriod3"),
                                                      RatePeriod4 = (string)listing.Element("RatePeriod4"),
                                                      Kitchen = (string)listing.Element("Kitchen"),
                                                      DiningRoom = (string)listing.Element("DiningRoom"),
                                                      Bedroom = (string)listing.Element("Bedroom"),
                                                      Bathroom = (string)listing.Element("Bathroom"),
                                                      MaxOccupants = (int)listing.Element("MaxOccupants")
                                                  }).ToList();
                }
            }


    However, when it get to the OpenReadCompleted event, e.Error is the following:  System.Net.WebException: The remote server returned an error: NotFound ---> System.Net.WebException: The remote server returned an error: NotFound....

    Any ideas what is going on or how to fix this?

    Thanks,
    Greg

    Tuesday, March 22, 2011 12:07 AM

Answers

  • Do I really need to create a webservice in order to read an XML file that is being deployed with the application?  I guess it kind of makes sense since the xap file runs on the client's browser and not the server like asp.net. 

    No, you don't have to do that. 

    Wait, where is your XML file? Is it inside the XAP or is it under the Asset Folder of your Web project?

    If it is in your XAP, you certainly can't  use Web service nor WebClient.DownloadAsych call to get it. You use XDocument.Load to load it.

    If the file is under your Web Project which means it is still on your server, you use WebClient.DoaloadAsych call to get it. Then see my last post.


    Tuesday, March 22, 2011 10:26 AM

All replies

  • Dear,

    Assets/xml/units.xml

    i think that the above file is at client side, then why are you seding the uri to server, you can read any xml file at client side also.

    XDocument xdoc=XDocument.Load("units.xml");

    Tuesday, March 22, 2011 12:56 AM
  • The file will not be client side.  It is on the server.

    Tuesday, March 22, 2011 9:31 AM
  • webmethod in .asmx webservice
    [WebMethod]
            public string ReadXMLFile ()
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "XMLFile.xml"; // the file name xmlfile is at the server side
                XDocument xdoc = XDocument.Load(path); //Load in xdocument
                return xdoc.ToString();  // send in the form of string
            }


    In mainpage.xaml.cs
     public partial class MainPage : UserControl
        {
            ServiceReference1.WebService1SoapClient client = null;
            public MainPage ()
            {
                InitializeComponent();
                client = new ServiceReference1.WebService1SoapClient(); // creating the object of service reference
                this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            }
    
            void MainPage_Loaded (object sender, RoutedEventArgs e)
            {
                client.ReadXMLFileCompleted += new EventHandler<ServiceReference1.ReadXMLFileCompletedEventArgs>(client_ReadXMLFileCompleted);
                client.ReadXMLFileAsync(); // calling webservice on page load, this get the xml contents from the server
            }
    
            void client_ReadXMLFileCompleted (object sender, ServiceReference1.ReadXMLFileCompletedEventArgs e)
            {
                if(!string.IsNullOrEmpty(e.Result)) // here oupt is in string format
                {
                    XDocument xdoc = XDocument.Parse(e.Result); // convert string into xml, finally the you read the xml file and got the output to the client side
                }
                else
                {
                }
            }
        }


    Tuesday, March 22, 2011 10:10 AM
  • Try this:

    wClient
    .OpenReadAsync(new Uri("../Assets/xml/units.xml", UriKind.Relative));

    Tuesday, March 22, 2011 10:24 AM
  • Do I really need to create a webservice in order to read an XML file that is being deployed with the application?  I guess it kind of makes sense since the xap file runs on the client's browser and not the server like asp.net. 

    What if I were to include the xml file inside the xap file?  Then how could I read it?

    Tuesday, March 22, 2011 10:24 AM
  • Do I really need to create a webservice in order to read an XML file that is being deployed with the application?  I guess it kind of makes sense since the xap file runs on the client's browser and not the server like asp.net. 

    No, you don't have to do that. 

    Wait, where is your XML file? Is it inside the XAP or is it under the Asset Folder of your Web project?

    If it is in your XAP, you certainly can't  use Web service nor WebClient.DownloadAsych call to get it. You use XDocument.Load to load it.

    If the file is under your Web Project which means it is still on your server, you use WebClient.DoaloadAsych call to get it. Then see my last post.


    Tuesday, March 22, 2011 10:26 AM
  • Nope, doesn't work sladapter.

    Tuesday, March 22, 2011 10:28 AM
  • OK, here is what I have.  My page is located inside the Views folder.  The codebehind for that page needs to read the xml file located in Assets/xml/units.xml.  I have the xml files Build Action set to Content.  I have looked inside the xap file and it is there. 

    So, now all I want to do is read from that file using the code above.  I've tried all sorts of path variations to the xml file (/Assets/.., Assets/..., units.xml, ../Assets/...) and I keep coming back with the same Exception error of NotFound.

    I'm reading this file immediately after the InitializeComponent function when the page loads.  The page is dependent on reading this information otherwise the page will not load with the needed content.  Could that be the problem?  I doubt it since it is throwing an error but figured I'd ask.

    Update: Ideally, I would like it on the server so that I could update it as needed without redeploying my xap.  I've tried the DownloadAsync method and couldn't get it to work either.  Either way, I don't care at this point.

    Tuesday, March 22, 2011 10:36 AM
  • When the XML file in under your XAP, do not use WebClient to get it.  Use Xdocuemnt.Load to load it.

    Update: Ideally, I would like it on the server so that I could update it as needed without redeploying my xap.  I've tried the DownloadAsync method and couldn't get it to work either.  Either way, I don't care at this point.

    If you do not want to deploy the xml file with your XAP, move the Xml folder to your Web project now.

    Say your XML folder is under Web Project root:

      wClient.OpenReadAsync(new Uri("../xml/units.xml", UriKind.Relative));


    Tuesday, March 22, 2011 10:53 AM
  • ooohhhhhhhhh dear, then y did u said me that xml file is at server side?

    I've tried all sorts of path variations to the xml file (/Assets/.., Assets/..., units.xml, ../Assets/...)

    as per this statement and for your knowledge, let me tell you, thar your xml file is at client side.

    Tuesday, March 22, 2011 11:56 AM
  • OK.  I understand now.

    So, I am trying to load it from the xap file.  I checked my xap file and the file exists under Assets/xml/units.xml.  I didn't do any of the WebClient code and just did the following:

    XDocument xDoc = XDocument.Load("Assets/xml/units.xml", LoadOptions.None);

    I get the error message that it can't find the file in the xap.  So, I tried the following with the same results:

    /Assets/xml/units.xml, units.xml, ../Assets/xml/units.xml

    What am I missing here?

    Tuesday, March 22, 2011 3:51 PM
  • The first path you used should work. Are you sure the file is in the XAP? Is its Build Action set to Content?

    Tuesday, March 22, 2011 4:01 PM
  • The Build Action on the file is set to Content.  It is in the xap file.  I renamed it .zip and looked at it myself.

    Assets/xml/units.xml did not work.

    Tuesday, March 22, 2011 4:12 PM
  • Could you post the error details you got?

    Tuesday, March 22, 2011 4:17 PM
  • OK, something must have been cached.  It is now working with loading the XDocument and using Assets/xml/units.xml.

    Whew!  Thanks everyone.  I'll try using the download method later as I would really like that option better so I can just modify the xml file and not have to recompile the code to make minor changes.

    Thanks,
    Greg

    Tuesday, March 22, 2011 4:58 PM
  • You don't need to recompile. Just change the XML file in your XAP file.

    Tuesday, March 22, 2011 5:07 PM
  • It is working.  No, I had to change the path to the xml file and recompile and it wasn't taking for some reason.

    Anyway, all is working fine.

    Tuesday, March 22, 2011 5:15 PM