locked
UriFormatException was Unhandled

    Question

  • Hello!

     

    I have the following function, which opens an XML file using System.XML.Linq.XElement:

     

    public int openXML(string filePath)
            {

                // the object: string filePath is not used (yet) in this function


                // open the XML document. If filePath is null, use the default path
                // of http://intranet.westlands.torbay.sch.uk/frog/common/io/news.xml

                // first, attempt to open without proxy authentication. If this fails,
                // catch the error and open the document with proxy authentication using
                // the default credentials that the end user is logged in with. This works
                // with users who have logged in to the domain. Local users will be
                // promtped for a username and password

                try
                {
                    // first. attempt with no proxy information
                    // escape char for "/" is "//", hence double foward slash
                    xmlDocument = XElement.Load("http:////intranet.company_website.sch.uk//frog//common//io//news.xml");

                }
                catch
                {
                    // proxy authentication error. Catch this error, and attempt to connect to
                    // XML resource using a proxy and the AD credentials

                    // proxy server located at IP 10.2.99.252. Use port 8080

                    System.Net.WebProxy proxyObject = new System.Net.WebProxy("10.2.99.252:8080");
                    proxyObject.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    System.Net.WebRequest.DefaultWebProxy = proxyObject;
                    xmlDocument = XElement.Load("http:////intranet.company_website.sch.uk//frog//common//io//news.xml"); // <-- error occurs here
                    return 0;
                }
                finally
                {

                }
                return 0;
            }

     

    xmlDocument is a global obect for the class that this function is in. Firstly, I call this function from another class (Windows Form) using the following code:

     

    Informatron tron = new Informatron();

    tron.OpenXML("")   // <-- to provide the string required, which like a said, isn't used just yet

     

    ...and the document opens correctly, with no errors

     

    However, when I call this code again, from the same Windows Form but from a different event, the following error occurs:

     

    UriFormatException was unhandled - Invalid URI: The hostname couldn not be parsed.

     

    The stack trace for this error is:

     

    at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)\r\n  
    at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri)\r\n  
    at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext)\r\n  
    at System.Xml.Linq.XElement.Load(String uri, LoadOptions options)\r\n  
    at Informatron.informatron_tron.openXML(String filePath)
    in \\\\fileserverl\\My Documents\\Visual Studio 2008\\Projects\\Informatron\\Informatron\\informatron_tron.cs:line 52\r\n  
    at Informatron.frmInformatron.linkPrevious_LinkClicked(Object sender, LinkLabelLinkClickedEventArgs e)
    in \\\\fileserver\\mpepprell\\My Documents\\Visual Studio 2008\\Projects\\Informatron\\Informatron\\frmInformatron.cs:line 49\r\n  
    at System.Windows.Forms.LinkLabel.OnMouseUp(MouseEventArgs e)\r\n  
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)\r\n  
    at System.Windows.Forms.Control.WndProc(Message& m)\r\n  
    at System.Windows.Forms.Label.WndProc(Message& m)\r\n  
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n  
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n  
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n  
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)\r\n  
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n  
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n  
    at Informatron.Program.Main()
    in \\\\fileserver\\mpepprell\\My Documents\\Visual Studio 2008\\Projects\\Informatron\\Informatron\\Program.cs:line 19\r\n  
    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n  
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n  
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.ThreadHelper.ThreadStart()"

     

    I am totally lost as to why this exception is occuring, and thus, I require help .. please?

     

     

    Thursday, November 11, 2010 1:27 PM

Answers

  • Hi 

    Thanks for your post!

    According to your context I guess the issue might be:

    xmlDocument = XElement.Load("http:////intranet.company_website.sch.uk//frog//common//io//news.xml");

    Correct code should be:
    xmlDocument = XElement.Load("http://intranet.company_website.sch.uk/frog/common/io/news.xml");


    Please note: Slash mark does NOT need to escape

    Please feel free to let me know if you have any further issues, thanks!


    Happy Coding:)
    Wayne Ye - Senior Software Development Engineer
    Personal Website: http://WayneYe.com


    • Proposed as answer by Roahn Luo Wednesday, November 17, 2010 10:58 AM
    • Marked as answer by Matt Pepprell Wednesday, November 17, 2010 11:16 AM
    Thursday, November 11, 2010 2:31 PM

All replies

  • Hi 

    Thanks for your post!

    According to your context I guess the issue might be:

    xmlDocument = XElement.Load("http:////intranet.company_website.sch.uk//frog//common//io//news.xml");

    Correct code should be:
    xmlDocument = XElement.Load("http://intranet.company_website.sch.uk/frog/common/io/news.xml");


    Please note: Slash mark does NOT need to escape

    Please feel free to let me know if you have any further issues, thanks!


    Happy Coding:)
    Wayne Ye - Senior Software Development Engineer
    Personal Website: http://WayneYe.com


    • Proposed as answer by Roahn Luo Wednesday, November 17, 2010 10:58 AM
    • Marked as answer by Matt Pepprell Wednesday, November 17, 2010 11:16 AM
    Thursday, November 11, 2010 2:31 PM
  • Ok... this works just fine. Now, I feel somewhat silly :(.

     

    Tiredness must be setting in. Anyway, thankyou for pointing me in the right direction and for a quick reply.

     

    -- Matt

     

    Thursday, November 11, 2010 3:11 PM
  • Hi Matt Pepprell,

     

    Don't say that, you are ABSOLUTELY not silly, I make mistakes all the time, just work hard and make mistakes fewer and fewer, then you got improved:)

     

    Happy coding!

     

    PS, if your question got solved, please don't remember mark it as answered:)



    Happy Coding:)
    Wayne Ye - Senior Software Development Engineer
    Personal Website: http://WayneYe.com


    Thursday, November 11, 2010 3:15 PM
  • Hi Wayne

     

    Well... I improve.. and improve and learn new things all the time. But today.. I was totally lost as to why this was happening with the error only occuring the second time I call this function.

     

    Like I said, thankyou for your help. Everything is working just nicely now

     

    Take care,

     

    -- Matt

     

    Thursday, November 11, 2010 3:20 PM