C#: getting site's encoding for WebClient beforehand
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 pos开发者_运维问答sible to do?
Thank you
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.
Simply use this now and it should handle everything own his own:
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
XDocument.Load(myHttpWebResponse.GetResponseStream());
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx
精彩评论