从俄罗斯网站读取 XML 时的编码问题
Encoding problems when reading XML from a Russian website
我从一家俄罗斯银行的 Web 服务(来源:http://www.cbr.ru/scripts/XML_daily.asp)获取 xml 中的值
我的ASP.NET代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" CodePage="65001" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="volutes" runat="server" >
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</form>
</body>
</html>
我的 C# 代码:
DataTable dt = new DataTable();
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://www.cbr.ru/scripts/XML_daily.asp");
StreamReader reader = new StreamReader(stream);
String content = reader.ReadToEnd();
XmlDocument xml = new XmlDocument();
xml.LoadXml(content);
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Value", typeof(string));
XmlNodeList nodeList = xml.SelectNodes("/ValCurs/Valute");
foreach (XmlNode node in nodeList)
{
DataRow dtrow = dt.NewRow();
dtrow["Name"] = node["Name"].InnerText;
dtrow["Value"] = node["Value"].InnerText;
dt.Rows.Add(dtrow);
}
volutes.DataSource = dt;
volutes.DataBind();
在我看到的结果页面中:
Name Value
������������� ������ 46,0642
为什么?
您应该为 StreamReader
使用正确的编码并将其传递到 constructor,否则 reader 将默认使用 UTF-16 LE:
using (StreamReader reader = new StreamReader( stream
, Encoding.GetEncoding("windows-1251")
)
)
{
}
我从一家俄罗斯银行的 Web 服务(来源:http://www.cbr.ru/scripts/XML_daily.asp)获取 xml 中的值
我的ASP.NET代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" CodePage="65001" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="volutes" runat="server" >
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</form>
</body>
</html>
我的 C# 代码:
DataTable dt = new DataTable();
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://www.cbr.ru/scripts/XML_daily.asp");
StreamReader reader = new StreamReader(stream);
String content = reader.ReadToEnd();
XmlDocument xml = new XmlDocument();
xml.LoadXml(content);
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Value", typeof(string));
XmlNodeList nodeList = xml.SelectNodes("/ValCurs/Valute");
foreach (XmlNode node in nodeList)
{
DataRow dtrow = dt.NewRow();
dtrow["Name"] = node["Name"].InnerText;
dtrow["Value"] = node["Value"].InnerText;
dt.Rows.Add(dtrow);
}
volutes.DataSource = dt;
volutes.DataBind();
在我看到的结果页面中:
Name Value
������������� ������ 46,0642
为什么?
您应该为 StreamReader
使用正确的编码并将其传递到 constructor,否则 reader 将默认使用 UTF-16 LE:
using (StreamReader reader = new StreamReader( stream
, Encoding.GetEncoding("windows-1251")
)
)
{
}