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: