Search code examples
c#xmlxmldocument

System.UriFormatException: 'Invalid URI: The Uri scheme is too long.'


I working on a small project where I am trying to read a response from a websocket and trying to load it in xmlDoc for further operations.

The following are the snippet of my code, where i get the "System.UriFormatException". The response is of XMLType

        UTF8Encoding encoder = new UTF8Encoding();
        byte[] buffer;

        buffer = encoder.GetBytes("<XML Response from a websocket>");

        string xml = Encoding.UTF8.GetString(buffer);

        XmlDocument xmlDoc = new XmlDocument();

        xmlDoc.Load(xml);

Is there any way by which we could overcome the exception, or what am i doing wrong in this code.


Solution

  • Lets visit the documentation

    XmlDocument.Load(String)

    Loads the XML document from the specified URL.

    xmlDoc.Load(xml);
    

    xml is not a Url

    What you are most likely looking for is

    XmlDocument.LoadXml(String) Method

    Loads the XML document from the specified string.