如何将UTF16字符串转换为UTF8? [英] How to convert UTF16 string to UTF8?

查看:135
本文介绍了如何将UTF16字符串转换为UTF8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个XML文件。它花了

 <? xml version =   1.0 encoding =   utf-16 ?>  

但我想

 <? xml版本=   1.0 encoding =  < span class =code-string> utf-8 ?>  

我如何转换它?



这是我的代码。



我更新了我的代码。现在请看一下。并且警告消息是

 DOMDocument :: loadXML():文档标记为UTF-16但在实体中具有UTF-8内容





 XmlDocument xmlDoc =  new  XmlDocument(); 

XmlElement xmlElmdetails = xmlDoc.CreateElement( details);
xmlDoc.AppendChild(xmlElmdetails);

XmlElement xmlName = xmlDoc.CreateElement( name);
xmlElmdetails.AppendChild(xmlName);
xmlName.InnerText = Jack;

XmlElement xmlPhone = xmlDoc.CreateElement( phone);
xmlElmdetails.AppendChild(xmlPhone);

XmlElement xmlHome = xmlDoc.CreateElement( home);
xmlPhone.AppendChild(xmlHome);
xmlHome.InnerText = 9876543210;




使用(StringWriter sr = new StringWriter())
{
XmlWriterSettings xws = new XmlWriterSettings();
xws.Indent = true ;

使用(XmlWriter xtw = XmlTextWriter.Create(sr,xws))
{
xmlDoc.WriteTo(xtw );
}
Response.Write(sr.ToString());
}

解决方案

使用 XmlWriterSettings.Encodeing 属性使用您想要的编码:

 {
XmlWriterSettings xws = new XmlWriterSettings();
xws.Indent = true;
xws.Encoding = Encoding.UTF8;

// ...
}

这将导致您的应用程序编写UTF-8编码的XML。



但是 UTF-8已经是上述属性的标准值。所以问题仍然是UTF-16编码的XML如何首先出现。

您的应用程序是创建它还是从某个地方获取它?


< blockquote class =FQ>

Encoding属性仅适用于使用指定的Stream或指定的文件名创建的XmlWriter实例。如果使用指定的TextWriter创建XmlWriter实例,则底层TextWriter的编码将覆盖Encoding属性。例如,如果此属性设置为特定XmlWriter的Unicode(UTF-16),但底层编写器是StreamWriter(源自TextWriter),其编码设置为UTF8,则输出将为UTF-8编码。 / blockquote>字符串在内部是utf-16,因此您必须更改StringWriter的编码...


 Response.Write(sr.ToString()) ; 



什么是响应?看起来像一些网络东西。

它有一个编码属性吗?如果是这样,请正确设置。


I create a XML file. It took

<?xml version="1.0" encoding="utf-16" ?>

but I want

<?xml version="1.0" encoding="utf-8" ?>

How i convert it?

Here is my code.

I update my code. Now please look it. And warning msg is

DOMDocument::loadXML(): Document labelled UTF-16 but has UTF-8 content in Entity



XmlDocument xmlDoc = new XmlDocument();

XmlElement xmlElmdetails = xmlDoc.CreateElement("details");
xmlDoc.AppendChild(xmlElmdetails);

XmlElement xmlName = xmlDoc.CreateElement("name");
xmlElmdetails.AppendChild(xmlName);
xmlName.InnerText = "Jack";

XmlElement xmlPhone = xmlDoc.CreateElement("phone");
xmlElmdetails.AppendChild(xmlPhone);

XmlElement xmlHome = xmlDoc.CreateElement("home");
xmlPhone.AppendChild(xmlHome);
xmlHome.InnerText = "9876543210";




 using (StringWriter sr = new StringWriter())
            {
                XmlWriterSettings xws = new XmlWriterSettings();
                xws.Indent = true;

                using (XmlWriter xtw = XmlTextWriter.Create(sr, xws))
                {
                    xmlDoc.WriteTo(xtw);
                }
                Response.Write(sr.ToString());
            }

解决方案

Use the XmlWriterSettings.Encodeing property to use the encoding you want:

{
    XmlWriterSettings xws = new XmlWriterSettings();
    xws.Indent = true;
    xws.Encoding = Encoding.UTF8;

    // ...
}

This will cause your application to write UTF-8 encoded XML.

But UTF-8 already is the mentioned property's standard value. So the question remains how the UTF-16 encoded XML arose in the first place.
Does your application create it or do you get it from somewhere?


The Encoding property only applies to the XmlWriter instances that are created either with the specified Stream or with the specified file name. If the XmlWriter instance is created with the specified TextWriter, the Encoding property is overridden by the encoding of the underlying TextWriter. For example, if this property is set to Unicode (UTF-16) for a particular XmlWriter, but the underlying writer is a StreamWriter (which derives from TextWriter) with its encoding set to UTF8, the output will be UTF-8 encoded.

String are utf-16 internally, so you have to change the encoding of your StringWriter...


Response.Write(sr.ToString());


What's that Response? Looks like some web thingy.
Does it have an Encoding property? If so, set it properly.


这篇关于如何将UTF16字符串转换为UTF8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆