C# XmlReader encoding defined as utf-8 but have Is

2019-09-09 10:01发布

I have a new problem.

First, thank you for all replies and help, I really appreciate!

So, my actual problem is: The state release a model of xml file that need be filled and keep the model data with UTF-8 encoding! When the people fill the data on software, they use some characters that are in ISO-8859-1 encode type like: Ç Õ Á and generate the file again, continue saved as UTF-8 and use the accents.

My program that process the data use this code:

    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;

So, when I try to read the file I can't read because do not open the file.

I have two options to read properly: Option 1: Before open in my program, I need change <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

TO

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

But this is illegal.

Or I need change the codes like this foto to other without accents (that give too much work to do): Image removing accents

So, why to process the text on C# with ISO-8859-1 (read the xml accents) without change the encoding type on XML file?

Thank you

PS: I look in suggested threads but don't finish a problem like mine.

Thanks

1条回答
可以哭但决不认输i
2楼-- · 2019-09-09 10:17

I changed encoding to 1252 and everything works.

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;
                            }
                        }
                    }
                }
            }
        }
    }
}
查看更多
登录 后发表回答