C#XmlReader编码定义为utf-8,但文件上具有Iso-8859-1字符 [英] C# XmlReader encoding defined as utf-8 but have Iso-8859-1 characters on file

查看:242
本文介绍了C#XmlReader编码定义为utf-8,但文件上具有Iso-8859-1字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个新问题。



首先,感谢您的所有答复和帮助,我非常感谢!



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

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

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

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

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

table.Columns.AddRange(columns);

foreach(行中的可变行)
{
table.Rows.Add(row);
}
// //最基本的XML格式XML exibindo seus dados。
dataGridView1.DataSource =表;

因此,当我尝试读取文件时,由于无法打开文件而无法读取。



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



TO

 <?xml版本= 1.0编码= ISO-8859-1独立=是吗? 

但这是非法的。



或者我需要将类似此foto的代码更改为其他不带重音符号的代码(不做太多工作):
图像去除重音符号



因此,为什么要使用ISO-8859-1在C#上处理文本(读取xml重音符号)而不更改编码



谢谢



PS:我在建议的线程中查找,但没有完成



谢谢

解决方案

我将编码更改为1252

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
使用System.Data;

名称空间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);
字典< string,string> colDict =新的Dictionary< string,string>();
而(!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);
开关(字段类型)
{
case string:
dt.Columns.Add(attrname,typeof(string));
休息时间;
case i4:
dt.Columns.Add(attrname,typeof(int));
休息时间;
}
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(在row.Attributes()中为XAttribute attrib)
{
string colName = attrib.Name.LocalName;
if(colDict.ContainsKey(colName))
{
开关(colDict [colName])
{
情况字符串:
newRow [colName ] =(string)attrib;
休息时间;
case i4:
newRow [colName] =(int)attrib;
休息时间;
}
}
}
}
}
}
}
}


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

解决方案

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;
                            }
                        }
                    }
                }
            }
        }
    }
}

这篇关于C#XmlReader编码定义为utf-8,但文件上具有Iso-8859-1字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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