c# - 以UTF-16编码格式反序列化xml文件时出现XmlException

标签 c# xml encoding xmlserializer xmlexception

使用 C# 的 XmlSerializer。

在反序列化给定文件夹中的所有 xml 文件的过程中,我看到 XmlException “XML 文档 (0, 0)”中存在错误。 InnerException 是 “没有 Unicode 字节顺序标记。无法切换到 Unicode”。

目录中的所有 xml 都是“UTF-16”编码的。唯一的区别是,某些 xml 文件缺少在反序列化时我正在使用其对象的类中定义的元素。

例如,假设我的文件夹中有 3 种不同类型的 xml:

文件1.xml

<?xml version="1.0" encoding="utf-16"?>
<ns0:PaymentStatus xmlns:ns0="http://my.PaymentStatus">
</ns0:PaymentStatus>

文件2.xml

<?xml version="1.0" encoding="utf-16"?>
<ns0:PaymentStatus xmlns:ns0="http://my.PaymentStatus">
<PaymentStatus2 RowNum="1" FeedID="38" />
</ns0:PaymentStatus>

文件3.xml

<?xml version="1.0" encoding="utf-16"?>
<ns0:PaymentStatus xmlns:ns0="http://my.PaymentStatus">
<PaymentStatus2 RowNum="1" FeedID="38" />
<PaymentStatus2 RowNum="2" FeedID="39" Amt="26.0000" />
</ns0:PaymentStatus>

我有一个类来表示上面的 xml:

[XmlTypeAttribute(AnonymousType = true, Namespace = "http://my.PaymentStatus")]
[XmlRootAttribute("PaymentStatus", Namespace = "http://http://my.PaymentStatus", IsNullable = true)]
public class PaymentStatus
{

    private PaymentStatus2[] PaymentStatus2Field;

    [XmlElementAttribute("PaymentStatus2", Namespace = "")]
    public PaymentStatus2[] PaymentStatus2 { get; set; }

    public PaymentStatus()
    {
        PaymentStatus2Field = null;
    }
}

[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "", IsNullable = true)]

public class PaymentStatus2
{

    private byte rowNumField;
    private byte feedIDField;
    private decimal AmtField;
    public PaymentStatus2()
    {
        rowNumField = 0;
        feedIDField = 0;
        AmtField = 0.0M;
    }

    [XmlAttributeAttribute()]
    public byte RowNum { get; set; }

    [XmlAttributeAttribute()]
    public byte FeedID { get; set; }
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public decimal Amt { get; set; }
}

以下代码片段为我进行了反序列化:

foreach (string f in filePaths)
{
  XmlSerializer xsw = new XmlSerializer(typeof(PaymentStatus));
  FileStream fs = new FileStream(f, FileMode.Open);
  PaymentStatus config = (PaymentStatus)xsw.Deserialize(new XmlTextReader(fs));
}

我错过了什么吗?它必须是编码格式的东西,因为当我尝试用 UTF-8 手动替换 UTF-16 时,这似乎工作得很好。

最佳答案

我今天在使用第三方网络服务时遇到了同样的错误。

我听从了 Alexei 的建议,使用了 StreamReader 并设置了编码。之后,可以在 XmlTextReader 构造函数中使用 StreamReader。这是使用原始问题中的代码实现的:

foreach (string f in filePaths)
{
  XmlSerializer xsw = new XmlSerializer(typeof(PaymentStatus));
  FileStream fs = new FileStream(f, FileMode.Open);
  StreamReader stream = new StreamReader(fs, Encoding.UTF8);
  PaymentStatus config = (PaymentStatus)xsw.Deserialize(new XmlTextReader(stream));
}

关于c# - 以UTF-16编码格式反序列化xml文件时出现XmlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25298355/

相关文章:

c - FFmpeg - 音频编码在音频上产生额外的噪音

python - Sklearn - 无法在随机森林分类器中使用编码数据

java - 哪个是最好的媒体容器?

c# - Kendo 小部件和 Bootstrap 输入组插件

mysql - 将多个 xml 传递给存储过程并检索 xml 属性

xml - ADF 复制事件将 XML 列插入到 Azure SQL DB 失败

php - 我将 Goodreads API 结果从 XML 转换为 JSON 的方法出了什么问题?

c# - 仅使用新记录进行更新时,DataTable.Load 方法不起作用

c# - 为什么一个属性被认为是不明确的,而另一个接口(interface)是只设置的?

c# - 简单聚类算法 2D。检测点簇