XmlException而在UTF-16编码格式反序列化XML文件 [英] XmlException while deserializing xml file in UTF-16 encoding format

查看:2544
本文介绍了XmlException而在UTF-16编码格式反序列化XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#的XmlSerializer的。



在反序列化给定文件夹中的所有XML文件的过程中,我看到XmlException
有XML文档中的一个错误(0,0)。和的InnerException是没有Unicode字节顺序标记。不能切换到Unicode。搜索结果
在目录中的所有XML的都是UTF-16编码。唯一的区别是,一些文件有缺失的元素,其目的我使用,而反序列化类中定义的。



例如,请考虑我有3个不同类型的XML的的在我的文件夹:



file1.xml

 < XML版本=1.0编码=UTF-16>?; 
< NS0:PaymentStatus的xmlns:NS0 =HTTP://my.PaymentStatus>
< / NS0:PaymentStatus>



file2.xml

 <?XML版本=1.0编码=UTF-16>?; 
< NS0:PaymentStatus的xmlns:NS0 =HTTP://my.PaymentStatus>
< PaymentStatus2的rownum =1FeedID =38/>
< / NS0:PaymentStatus>



file3.xml

 <?XML版本=1.0编码=UTF-16>?; 
< NS0:PaymentStatus的xmlns:NS0 =HTTP://my.PaymentStatus>
< PaymentStatus2的rownum =1FeedID =38/>
< PaymentStatus2的rownum =2FeedID =39金额=26.0000/>
< / NS0:PaymentStatus>



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

  [XmlTypeAttribute(AnonymousType = TRUE,命名空间=HTTP://my.PaymentStatus)] 
[XmlRootAttribute(PaymentStatus,命名空间=HTTP:/ /http://my.PaymentStatus,ISNULLABLE = TRUE)]
公共类PaymentStatus
{

私人PaymentStatus2 [] PaymentStatus2Field;

[XmlElementAttribute(PaymentStatus2,命名空间=)]
公共PaymentStatus2 [] {PaymentStatus2获得;组; }

公共PaymentStatus()
{
PaymentStatus2Field = NULL;
}
}

[XmlTypeAttribute(AnonymousType =真)]
[XmlRootAttribute(命名空间=,ISNULLABLE =真)]

酒店的公共类PaymentStatus2
{

专用字节rowNumField;
专用字节feedIDField;
私人小数AmtField;
公共PaymentStatus2()
{
rowNumField = 0;
feedIDField = 0;
AmtField = 0.0M;
}

[XmlAttributeAttribute()]
公共字节ROWNUM {搞定;组; }

[XmlAttributeAttribute()]
公共字节FeedID {搞定;组; }
[System.Xml.Serialization.XmlAttributeAttribute()]
公共小数金额{搞定;组; }
}



下面的代码片段确实对我反序列化:

 的foreach(串f在文件路径)
{
XmlSerializer的XSW =新的XmlSerializer(typeof运算(PaymentStatus));
的FileStream FS =新的FileStream(F,FileMode.Open);
PaymentStatus配置=(PaymentStatus)xsw.Deserialize(新的XmlTextReader(FS));
}



我缺少的东西吗?它必须是一些与编码格式,因为当我尝试用UTF-8手动替换UTF-16这似乎工作得很好。


解决方案

我今天遇到了这同样的错误与第三方Web服务的工作。



我用一个StreamReader和设置编码遵循阿列克谢的意见。此后,该StreamReader的可以在XmlTextReader的构造函数中。下面是使用代码从原来的质询的实现:



<预类=郎-CS prettyprint-覆盖> 的foreach(串f的文件路径)
{
XmlSerializer的XSW =新的XmlSerializer(typeof运算(PaymentStatus));
的FileStream FS =新的FileStream(F,FileMode.Open);
StreamReader的流=新的StreamReader(FS,Encoding.UTF8);
PaymentStatus配置=(PaymentStatus)xsw.Deserialize(新XmlTextReader的(流));
}


Using C#'s XmlSerializer.

In process of deserializing all xml files in a given folder, I see XmlException "There is an error in XML document (0, 0)". and InnerException is "There is no Unicode byte order mark. Cannot switch to Unicode".

All the xmls in the directory are "UTF-16" encoded. Only difference being, some xml files have elements missing that are defined in the class whose object I am using while deserialization.

For example, consider I have 3 different types of xmls in my folder:

file1.xml

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

file2.xml

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

file3.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>

I have a class to represent the above 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; }
}

Following snippet does the deserializing for me:

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

Am I missing something? It has to be something with encoding format because when I try to manually replace UTF-16 by UTF-8 and that seems to work just fine.

解决方案

I ran into this same error today working with a third party web service.

I followed Alexei's advice by using a StreamReader and setting the encoding. After that the StreamReader can be used in the XmlTextReader constructor. Here's an implementation of this using the code from the original question:

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

这篇关于XmlException而在UTF-16编码格式反序列化XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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