简体   繁体   English

使用c#从xml获取文本时设置System.Net.Mail.MailMessage主体的编码

[英]setting the encoding of a System.Net.Mail.MailMessage body while getting text from xml using c#

I'm trying to set the encoding of a MailMessage body so it is able to display german "umlauts" (ä, ö, ü). 我正在尝试设置MailMessage主体的编码,以便它能够显示德语的“变音符”(ä,ö,ü)。 I get the message text from an xml file. 我从xml文件中获取消息文本。 The encoding of the xml is iso-8859-1 and also is the MailMessage body. xml的编码是iso-8859-1 ,也是MailMessage主体。

MailMessage msg = new MailMessage();
msg.From = new MailAddress(sFrom);
msg.To.Add(sTo);
msg.Subject = sSubject;
msg.Body = sBody;
msg.IsBodyHtml = true;
msg.BodyEncoding = Encoding.GetEncoding("iso-8859-1");

The first line of xml is this: xml的第一行是这样的:

<?xml version="1.0" encoding="iso-8859-1"?>

I also tried UTF8 and Unicode encoding, but that doesn't help neither. 我也尝试了UTF8Unicode编码,但这都无济于事。 The body is still unable to display umlauts. 身体仍然无法显示变音。

Is there anything special I have to notice or am I just using the wrong encoding? 我有什么特别需要注意的,还是我使用的编码错误?

EDIT: It's a Windows Forms application and I'm using .NET framework 4.5.2 编辑:这是一个Windows窗体应用程序,并且我正在使用.NET Framework 4.5.2

EDIT2: I'm using XmlSerializer to get the xml data. EDIT2:我正在使用XmlSerializer来获取xml数据。

XmlSerializer xSerializer = new XmlSerializer(typeof(App));

using (StreamReader srXml = new StreamReader(_sFileName))
{
    _oAppConfig = (App)xSerializer.Deserialize(srXml);
}

Note that App is a class I created for my own. 请注意, App是我为自己创建的类。

Try specifying the Encoding while reading the file such as: 尝试在读取文件时指定编码,例如:

XmlSerializer xSerializer = new XmlSerializer(typeof(App));

using (StreamReader srXml = new StreamReader(_sFileName, Encoding.GetEncoding("iso-8859-1")) )
{
    _oAppConfig = (App)xSerializer.Deserialize(srXml);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM