我正在尝试从XML Feed中提取£符号(完整版本在这里> http://mjsiphone.com/scotjobsnet/)
迷你版在这里> http://scotjobsnet.co.uk.ni.strategiesuk.net/testfeed.xml
我无法控制Feed或源服务器的来源或Feed的格式,他们使用的标题等等。
我只需要构建一个.NET 4.5控制台应用程序,它将提取数据并将其保存在我们自己的站点数据库中。
此外,我必须删除所有HTML(来自作业说明)并删除任何HTML编码字符并将其替换为实际值。
因此,我需要在MS SQL 2008数据库中保存nvarchar数据类型中的真实£符号,而不是20,000英镑或20,000英镑等。
查看Feed的来源时,顶部有UTF-8。
然而,当在浏览器源中查看提要时,我没有看到任何提及UTF-8作为请求/响应标头,并且在请求标头(Chrome)中我只看到:
接受语言:EN-GB,EN-US; Q = 0.8,连接; Q = 0.6
当我将浏览器或控制台中的字符复制并粘贴到SQL中并检查它们时,它们返回163,这是正确的ASCII字符编码,例如£
如果您在浏览器中查看Feed,则英镑符号显示正常。
当我将内容传输到Windows命令控制台时,它们会显示为£符号。
然而,当我尝试将它们保存到数据库或管道控制台调试到EditPlus中的文件(字符编码设置为UTF8或ASCII)时,我只是在数字前面得到正方形而不是符号,例如在CMD中
[.exe的路径]> [debug.log文件的路径]
控制台无法正确地将内容传递给编辑器,或者我需要使用正确的编码或传递更多标题或以不同方式提取XML。
以下是我用于测试此代码的示例,只使用一个字段,在其中使用£符号然后断开。
static void Main(string[] args)
{
Console.WriteLine("START");
XmlDocument xDoc = new XmlDocument();
string feedURL = "http://scotjobsnet.co.uk.ni.strategiesuk.net/testfeed.xml";
WebClient webClient = new WebClient();
// need to pass a user-agent > 10 Chars to prevent blocking by OUR servers 403
webClient.Headers.Add("user-agent", "Mozilla/5.0 (compatible; Job Feed Importer;)");
// piping out to console with this line below shows a £ but to a UTF-8 or ASCII file it's gibberish
webClient.Headers.Add("Content-Type", "application/xml; charset=utf-8");
// I tried this but still the console works but piping to an editor in UTF-8 or ASCII shows squares
webClient.Headers.Add("Accept-Language", "utf-8,en-GB,en-US;q=0.8,en;q=0.6");
// download as text - is this the problem? Should I be using a different method
string feedText = webClient.DownloadString(feedURL);
// load into XML object
xDoc.LoadXml(feedText);
if (xDoc != null)
{
XmlElement root = xDoc.DocumentElement;
XmlNodeList xNodelst = root.SelectNodes("job");
foreach (XmlNode node in xNodelst)
{
string salary = node.SelectSingleNode("candidateSalary").InnerText;
// piped to cmd console the £ signs show but to a UTF-8 file they are just squares
// I've tried adding the Encoding.UTF8 or Encoding.ASCII still no joy
// Console.WriteLine("candidateSalary = " + salary,Encoding.UTF8);
Console.WriteLine("candidateSalary = " + salary);
break;
}
}
Console.WriteLine("FINISH");
}
任何帮助将不胜感激。
我确信这只是我需要传递的标题,或者可能是将XML内容输出到编辑器的问题。
正如我之前在Windows控制台中查看输出所说的那样,£显示得很好。
由于
答案 0 :(得分:0)
我希望此命令的输出不是UTF-8:
Console.WriteLine(Console.OutputEncoding);
这里有两个转码操作:
UTF-8 > UTF-16 string > console encoding
in the XML specification描述了检测XML文档编码的正确方法。 XmlDocument 将为您完成此操作。
控制台编码可以设置为UTF-8,也可以直接将编码的字节序列化为STDOUT。
Console.OutputEncoding = System.Text.Encoding.UTF8;
XmlDocument xDoc = new XmlDocument();
string feedURL = "http://scotjobsnet.co.uk.ni.strategiesuk.net/testfeed.xml";
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/5.0 (compatible; Job Feed Importer;)");
byte[] feed = client.DownloadData(feedURL);
xDoc.Load(new MemoryStream(feed));
if (xDoc != null)
{
XmlElement root = xDoc.DocumentElement;
XmlNodeList xNodelst = root.SelectNodes("job");
foreach (XmlNode node in xNodelst)
{
string salary = node.SelectSingleNode("candidateSalary").InnerText;
Console.WriteLine("candidateSalary = " + salary);
break;
}
}
有关 cmd.exe 和Unicode here的更多信息。