[英]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.