简体   繁体   English

Xml解析错误:十六进制值是无效字符

[英]Xml parsing error: hexadecimal value is an invalid character

I try get xml file with this code: 我尝试使用以下代码获取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);

but I get exception as below: 但我得到例外如下:

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

So according to many similar questions on stackoverflow I try add this commented line, but then I get exception: 所以根据stackoverflow上的许多类似问题,我尝试添加这个注释行,但后来我得到异常:

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

What's wrong? 怎么了?

Assuming the compression that is being applied to the XML is GZip you can uncompress the XML like so: 假设正在应用于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);

If the GZipStream does not work to decompress the XML you'll have to replace it with the appropriate decompression stream. 如果GZipStream无法解压缩XML,则必须使用适当的解压缩流替换它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 十六进制值0x00是加载XML文档的无效字符 - Hexadecimal value 0x00 is a invalid character loading XML document 错误:十六进制值 0x00 是无效字符 c# - Error: hexadecimal value 0x00 is an invalid character c# 错误:XML文档中字符'',十六进制值0xb是非法的 - Error: Character ' ', hexadecimal value 0xb is illegal in XML documents 反序列化xml feed无效的十六进制字符 - Deserializing xml feed fails invalid hexadecimal character 十六进制值 0x07,是一个无效字符 - hexadecimal value 0x07, is an invalid character 十六进制值0x1E,是无效字符 - hexadecimal value 0x1E, is an invalid character 从Word粘贴+创建XML文档 - >十六进制值0x0C,是无效字符(.Net) - Paste from Word + Create XML document -> hexadecimal value 0x0C, is an invalid character (.Net) 发布到Azure错误 - 错误:'',十六进制值0x0F,是无效字符。 第1行,第285位 - Publish to Azure Error - Error : '', hexadecimal value 0x0F, is an invalid character. Line 1, position 285 导出到excel数据时出错:'',十六进制值0x07,使用C#是无效字符 - error while exporting to excel data : ' ', hexadecimal value 0x07, is an invalid character using c# 错误:'\',十六进制值 0x1F,是无效字符 - Error : '\u001f', hexadecimal value 0x1F, is an invalid character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM