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

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

我在做一个小项目,我试图从 websocket 读取响应并尝试将其加载到 xmlDoc 中以进行进一步操作。

以下是我的代码片段,其中我得到了 "System.UriFormatException"。响应是 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);

有什么方法可以克服异常,或者我在这段代码中做错了什么。

让我们访问文档

XmlDocument.Load(String)

Loads the XML document from the specified URL.

xmlDoc.Load(xml);

xml 不是 Url

您最有可能寻找的是

XmlDocument.LoadXml(String) Method

Loads the XML document from the specified string.