gpt4 book ai didi

c# - xml解析错误: hexadecimal value is an invalid character

转载 作者:太空宇宙 更新时间:2023-11-03 21:55:35 25 4
gpt4 key购买 nike

我尝试用这段代码获取 xml 文件:

HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
string xml = string.Empty;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
xml = sr.ReadToEnd();
}

XmlDocument xmlDoc = new XmlDocument();
//xml = xml.Replace((char)(0x1F), ' ');
xmlDoc.LoadXml(xml);

但我得到如下异常:

' ', hexadecimal value 0x1F, is an invalid character. Line 1, position 1.

因此,根据 stackoverflow 上的许多类似问题,我尝试添加此注释行,但随后出现异常:

Data at the root level is invalid. Line 1, position 2.

怎么了?

最佳答案

假设应用于 XML 的压缩是 GZip,您可以像这样解压缩 XML:

HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
string xml = string.Empty;
using (GZipStream gzip = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
using (StreamReader sr = new StreamReader(gzip))
{
xml = sr.ReadToEnd();
}

XmlDocument xmlDoc = new XmlDocument();
//xml = xml.Replace((char)(0x1F), ' ');
xmlDoc.LoadXml(xml);

如果 GZipStream 无法解压 XML,您必须将其替换为适当的解压流。

关于c# - xml解析错误: hexadecimal value is an invalid character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12467312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com