C# 如何删除ISO编码的XML文件中的断字符

C# 如何删除ISO编码的XML文件中的断字符,c#,xml,encoding,utf-16,iso-8859-1,C#,Xml,Encoding,Utf 16,Iso 8859 1,当我试图将UTF-16编码的xml文件转换为ISO-8859-1时,我看到了像这样的断字符 你能不能建议一些解决办法来删除坏掉的字符?我想要ISO编码格式的XML 这是我的密码 using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.AppSettings.Get("SqlConn"))) { sqlConnection.Open(); using (SqlCommand sqlCo

当我试图将UTF-16编码的xml文件转换为ISO-8859-1时,我看到了像
这样的断字符

你能不能建议一些解决办法来删除坏掉的字符?我想要ISO编码格式的XML

这是我的密码

using (SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.AppSettings.Get("SqlConn")))
{
    sqlConnection.Open();

    using (SqlCommand sqlCommand = new SqlCommand())
    {
        sqlCommand.CommandTimeout = 0;
        sqlCommand.CommandText = commandText;
        sqlCommand.Connection = sqlConnection;

        // the data from database data is UTF encoded
        using (StreamWriter textwriterISO = new StreamWriter(path + "_out.XML", false, Encoding.GetEncoding("ISO-8859-1")))
        {                                  
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            Console.WriteLine("Writing results.This could take a very long time.");
            while (sqlDataReader.Read())
            {
                for (int i = 0; i < sqlDataReader.FieldCount; i++)
                {
                    byte[] arr = System.Text.Encoding.GetEncoding(28591).GetBytes(sqlDataReader[i].ToString());
                    string ascii = Encoding.GetEncoding("UTF-8").GetString(arr);
                    textwriter.WriteLine(sqlDataReader.GetName(i),ascii));
                }

                textwriter.Flush();
            }
        }
    }                         
}
使用(SqlConnection SqlConnection=newsqlconnection(ConfigurationManager.AppSettings.Get(“SqlConn”))
{
sqlConnection.Open();
使用(SqlCommand SqlCommand=new SqlCommand())
{
sqlCommand.CommandTimeout=0;
sqlCommand.CommandText=CommandText;
sqlCommand.Connection=sqlConnection;
//数据库数据中的数据是UTF编码的
使用(StreamWriter textwriterISO=newstreamwriter(path+“_out.XML”),false,Encoding.GetEncoding(“ISO-8859-1”))
{                                  
SqlDataReader SqlDataReader=sqlCommand.ExecuteReader();
WriteLine(“写入结果。这可能需要很长时间。”);
while(sqlDataReader.Read())
{
对于(int i=0;i
您的代码误用了
StreamWriter
类,并对DB数据进行了错误的手动编码。您正在将源UTF-16 DB数据转换为CP28591,将CP28591字节解释为UTF-8以便将其转换回UTF-16,然后在写入文件时让
StreamWriter
将现在格式错误的UTF-16转换为ISO-8859-1。这是完全错误的做法,更不用说那些转换所浪费的开销了。让
StreamWriter
直接处理源UTF-16 DB数据的编码,去掉所有其他内容,例如:

using (StreamWriter textwriterISO = new StreamWriter(path + "_out.XML", false, Encoding.GetEncoding("ISO-8859-1")))
{                                  
    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
    Console.WriteLine("Writing results.This could take a very long time.");
    while (sqlDataReader.Read())
    {
        for (int i = 0; i < sqlDataReader.FieldCount; i++)
        {
            // you were originally calling the WriteLine(String, Object) overload.
            // Are you sure you want to call that? It interprets the first parameter
            // as a pattern to format the value of the second parameter. A DB column
            // name is not a formatting pattern!
            textwriterISO.WriteLine(sqlDataReader.GetName(i), sqlDataReader[i].ToString());

            // perhaps you meant to write the DB column name and field value separately?
            //
            // textwriterISO.WriteLine(sqlDataReader.GetName(i));
            // textwriterISO.WriteLine(sqlDataReader[i].ToString());
        }
        textwriterISO.Flush();
    }
}
使用(StreamWriter textwriterISO=newstreamwriter(path+“_out.XML”,false,Encoding.GetEncoding(“ISO-8859-1”))
{                                  
SqlDataReader SqlDataReader=sqlCommand.ExecuteReader();
WriteLine(“写入结果。这可能需要很长时间。”);
while(sqlDataReader.Read())
{
对于(int i=0;i

话虽如此,您提到希望以XML格式输出
StreamWriter本身不会为您输出XML。使用
XmlSerializer
XmlTextWriter
类将数据读取器数据转换为XML,然后写入
StreamWriter

您正在使用的部分代码,我们可以麻烦您吗?我们甚至不知道您使用的是哪种编程语言。@JLRishe,我正在使用C语言进行转换,您是否介意向我们展示您的代码,还是希望我们为您完成所有工作?谢谢您的建议,这对我很有帮助。
sqlDataReader[I]
使用什么编码?
textwriterISO
是否应该与
textwriter
相同?
sqlDataReader[i]
返回表示DB数据的
对象,不管它使用什么。
ToString()
将该数据转换为UTF-16字符串,但是DB需要进行该转换。是的,
textwriter
textwriterISO
应该是同一件事。