c#解析xml之后放到dataset中 中文的地方显示乱码 求解!!
一个xml文件里面存有一个string但是我用streamReader读取出来的,其中汉字显示的是乱码如何解决?...
一个xml文件里面存有一个string 但是我用streamReader读取出来的,其中汉字显示的是乱码 如何解决?
展开
2个回答
展开全部
这是你的文件:
openFileDialog1.Filter = "*.xml(*.xml)|*.xml";
string tradeName = textBox1.Text ;
FileStream fs=new FileStream(tradeName+".xml",FileMode.Open);
StreamWriter sr = new StreamWriter(fs);
string s = sr.ReadToEnd();
修改如下:
openFileDialog1.Filter = "*.xml(*.xml)|*.xml";
string tradeName = textBox1.Text ;
FileStream fs=new FileStream(tradeName,FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
string s = sr.ReadToEnd();
这样就OK了,但是看你的代码很乱,你可以参考下面的代码,测试过的:
OpenFileDialog openFiled = new OpenFileDialog();
openFiled.InitialDirectory = "d:\\";
openFiled.Filter = "*.xml(*.xml)|*.xml";
openFiled.FilterIndex = 1;
if (openFiled.ShowDialog() == DialogResult.OK)
{
string fname = openFiled.FileName;
FileStream fs = new FileStream(fname, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
string str = sr.ReadToEnd();
while (str.Length != 0 )
{
richTextBox1.Text += str;
}
sr.Close();
fs.Close();
}
openFileDialog1.Filter = "*.xml(*.xml)|*.xml";
string tradeName = textBox1.Text ;
FileStream fs=new FileStream(tradeName+".xml",FileMode.Open);
StreamWriter sr = new StreamWriter(fs);
string s = sr.ReadToEnd();
修改如下:
openFileDialog1.Filter = "*.xml(*.xml)|*.xml";
string tradeName = textBox1.Text ;
FileStream fs=new FileStream(tradeName,FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
string s = sr.ReadToEnd();
这样就OK了,但是看你的代码很乱,你可以参考下面的代码,测试过的:
OpenFileDialog openFiled = new OpenFileDialog();
openFiled.InitialDirectory = "d:\\";
openFiled.Filter = "*.xml(*.xml)|*.xml";
openFiled.FilterIndex = 1;
if (openFiled.ShowDialog() == DialogResult.OK)
{
string fname = openFiled.FileName;
FileStream fs = new FileStream(fname, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
string str = sr.ReadToEnd();
while (str.Length != 0 )
{
richTextBox1.Text += str;
}
sr.Close();
fs.Close();
}
展开全部
加encoding
System.Text.Encoding.Default
要看你文件的实际编码
System.Text.Encoding.Default
要看你文件的实际编码
追问
在哪+呢 我的代码:
openFileDialog1.Filter = "*.xml(*.xml)|*.xml";
string tradeName = textBox1.Text ;
FileStream fs=new FileStream(tradeName+".xml",FileMode.Open);
StreamWriter sr = new StreamWriter(fs);
string s = sr.ReadToEnd();
就这样 读取出来的字符串 汉字的地方就变成乱码呢!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询