如何将 byte[] 响应转换为有效的 XDocument?

发布于 2024-09-13 21:27:03 字数 791 浏览 7 评论 0原文

<?xml version="1.0" encoding="utf-8"?>
<rsp stat="ok">
        <image_hash>cxmHM</image_hash>
        <delete_hash>NNy6VNpiAA</delete_hash>
        <original_image>http://imgur.com/cxmHM.png</original_image>
        <large_thumbnail>http://imgur.com/cxmHMl.png</large_thumbnail>
        <small_thumbnail>http://imgur.com/cxmHMl.png</small_thumbnail>
        <imgur_page>http://imgur.com/cxmHM</imgur_page>
        <delete_page>http://imgur.com/delete/NNy6VNpiAA</delete_page>
</rsp>

这是我收到的典型回复。我已尝试以下操作,但收到一条错误消息,告诉我无法将非空白字符添加到内容中。

XDocument response = new XDocument(w.UploadValues("http://imgur.com/api/upload.xml", values));    
<?xml version="1.0" encoding="utf-8"?>
<rsp stat="ok">
        <image_hash>cxmHM</image_hash>
        <delete_hash>NNy6VNpiAA</delete_hash>
        <original_image>http://imgur.com/cxmHM.png</original_image>
        <large_thumbnail>http://imgur.com/cxmHMl.png</large_thumbnail>
        <small_thumbnail>http://imgur.com/cxmHMl.png</small_thumbnail>
        <imgur_page>http://imgur.com/cxmHM</imgur_page>
        <delete_page>http://imgur.com/delete/NNy6VNpiAA</delete_page>
</rsp>

This is the typical response I'll receive. I've tried the following but I get an error telling me that Non White Space Characters Cannot Be Added To Content.

XDocument response = new XDocument(w.UploadValues("http://imgur.com/api/upload.xml", values));    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

近箐 2024-09-20 21:27:03
byte[] responseData = w.UploadValues("http://imgur.com/api/upload.xml", values);
string responseText = Encoding.ASCII.GetString(responseData);  //  ASCII assumed
XDocument respnseXml = XDocument.Parse(responseText); 

但该错误也可能来自

byte[] responseData = w.UploadValues("http://imgur.com/api/upload.xml", values);
string responseText = Encoding.ASCII.GetString(responseData);  //  ASCII assumed
XDocument respnseXml = XDocument.Parse(responseText); 

But that Error could as well come from the values.

倾其所爱 2024-09-20 21:27:03

我的经验是,最好将字节加载到 MemoryStream 中并将其提供给 XDocument。这样您就不必解决任何空白问题。

byte[] responseData = w.UploadValues("http://imgur.com/api/upload.xml", values);
using(var ms = new MemoryStream(responseData)) 
{
var responseXml = XDocument.Load(ms);
}

I experienced it is better to load the bytes in a MemoryStream and feed that to the XDocument. This way you won't have to fix any white-space issues.

byte[] responseData = w.UploadValues("http://imgur.com/api/upload.xml", values);
using(var ms = new MemoryStream(responseData)) 
{
var responseXml = XDocument.Load(ms);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文