C#:预先获取 WebClient 的站点编码
我正在从 Internet 下载并解析大量 XML 文件。它们都有不同的编码,在第一行中进行了描述。
<?xml version="1.0" encoding="windows-1251"?>
<?xml version="1.0" encoding="UTF-8"?>
等等...
我需要设置正确的 WebClient.Encoding 参数才能以正确的编码接收文本。但如果不预先下载文件并阅读第一行,我就无法做到这一点。
可以做吗?
谢谢
I'm downloading and parsing a lot of XML files from Internet. They all have different encodings that are described on the first line.
<?xml version="1.0" encoding="windows-1251"?>
<?xml version="1.0" encoding="UTF-8"?>
and so on...
I need to set correct WebClient.Encoding parameter in order to receive the text in correct encoding. But I can't do that without pre-downloading the file and reading the first line.
Is it possible to do?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要设置任何内容 - 您根本不需要处理编码。只需获取二进制数据并让 XML 解析器来处理它。或者,如果您要将文件存储在磁盘上,只需将二进制数据直接转储到磁盘上即可。 您根本不需要担心编码。
You don't need to set anything - you don't need to handle the encoding at all. Just get the binary data and get the XML parsers to handle it. Or if you're going to store the files on disk, just dump the binary data straight onto disk. You don't need to worry about the encoding at all.
现在只需使用它,它应该处理自己的一切:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx
Simply use this now and it should handle everything own his own:
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx