【问题标题】:C# XmlReader encoding defined as utf-8 but have Iso-8859-1 characters on fileC# XmlReader 编码定义为 utf-8 但文件上有 Iso-8859-1 字符
【发布时间】:2016-08-16 14:54:40
【问题描述】:

我有一个新问题。

首先,感谢大家的回复和帮助,非常感谢!

所以,我的实际问题是:状态释放一个需要填充的xml文件模型,并以UTF-8编码保存模型数据! 当人们在软件上填写数据时,他们使用了一些 ISO-8859-1 编码类型的字符,如:Ç Õ Á 并再次生成文件,继续保存为 UTF-8 并使用重音符号。

我处理数据的程序使用以下代码:

    XmlReader xmlFile = XmlReader.Create(ofd.FileName, new XmlReaderSettings());
                ds.ReadXml(xmlFile);

                var doc = XDocument.Load(ofd.FileName);
                var columns = doc.Descendants("FIELD")
                    .Attributes("attrname")
                    .Select(fieldName => new DataColumn(fieldName.Value))
                    .ToArray();

                var rows = doc.Descendants("ROW")
                    .Select(row => columns.Select(col => (string)row.Attribute(col.ColumnName)).ToArray());
                var table = new DataTable();

                table.Columns.AddRange(columns);

                foreach (var row in rows)
                {
                    table.Rows.Add(row);
                }
                // Aqui ele mostra os dados das tabelas do arquivo XML exibindo seus dados.
                dataGridView1.DataSource = table;

所以,当我尝试读取文件时,我无法读取,因为没有打开文件。

我有两个选项可以正确阅读: 选项1:在我的程序中打开之前,我需要更改<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

到

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes" ?>

但这是非法的。

或者我需要将像这张照片这样的代码更改为其他没有重音符号的代码(这需要做太多工作): Image removing accents

那么,为什么要在 C# 上使用 ISO-8859-1 处理文本(读取 xml 重音符号)而不更改 XML 文件的编码类型?

谢谢

PS:我查看了建议的线程,但没有完成像我这样的问题。

谢谢

【问题讨论】:

  • 几周前我遇到了类似的问题。发现使用 streamreader 读取 xml 的第一行已解决问题。 StreamReader reader = new StreamReader(ofd.FileName); reader.ReadLine(); XmlReader xmlFile = XmlReader.Create(reader , new XmlReaderSettings()); ds.ReadXml(xmlFile);
  • 那么,我需要添加“new XmlReaderSettings("ISO-8859-1")”对吗?
  • 我不这么认为。跳过第一行。如果这不起作用,则将编码添加到 StreamReader,而不是 xml 阅读器。
  • 谢谢,我试试
  • 尝试这个并没有解决。还有其他解决方案吗?

标签: c# xml encoding utf-8 character-encoding


【解决方案1】:

我将编码更改为 1252,一切正常。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();

            StreamReader sReader = new StreamReader(FILENAME, Encoding.GetEncoding(1252));

            XmlReader reader = XmlReader.Create(sReader);
            Dictionary<string, string> colDict = new Dictionary<string, string>();
            while (!reader.EOF)
            {
                if (reader.Name != "FIELD")
                {
                    reader.ReadToFollowing("FIELD");
                }
                if (!reader.EOF)
                {
                    XElement field = (XElement)XElement.ReadFrom(reader);
                    string attrname = (string)field.Attribute("attrname");
                    string fieldtype = (string)field.Attribute("fieldtype");
                    switch (fieldtype)
                    {
                        case "string":
                            dt.Columns.Add(attrname, typeof(string));
                            break;
                        case "i4":
                            dt.Columns.Add(attrname, typeof(int));
                            break;
                    }
                    colDict.Add(attrname, fieldtype);
                }
            }
            reader.Close();
            sReader = new StreamReader(FILENAME, Encoding.GetEncoding(1252));
            reader = XmlReader.Create(sReader);
            while (!reader.EOF)
            {
                if (reader.Name != "ROW")
                {
                    reader.ReadToFollowing("ROW");
                }
                if (!reader.EOF)
                {
                    XElement row = (XElement)XElement.ReadFrom(reader);
                    DataRow newRow = dt.Rows.Add();
                    foreach (XAttribute attrib in row.Attributes())
                    {
                        string colName = attrib.Name.LocalName;
                        if (colDict.ContainsKey(colName))
                        {
                            switch (colDict[colName])
                            {
                                case "string":
                                    newRow[colName] = (string)attrib;
                                    break;
                                case "i4":
                                    newRow[colName] = (int)attrib;
                                    break;
                            }
                        }
                    }
                }
            }
        }
    }
}

【讨论】:

  • 代码现在使用编码 1252。
  • 谢谢老兄。你救了我。谢谢你!我不知道该说什么来感谢你!真的很感激!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
  • 2010-12-08
  • 2014-08-29
  • 1970-01-01
相关资源
最近更新 更多