Tag: xmlexception

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

使用C#的XmlSerializer。 在反序列化给定文件夹中的所有xml文件的过程中,我看到XmlException “There is an error in XML document (0, 0)”. 和InnerException是”There is no Unicode byte order mark. Cannot switch to Unicode”. 目录中的所有xmls都是“UTF-16”编码的。 唯一不同的是,一些xml文件缺少在反序列化时我正在使用的对象类中定义的元素。 例如,考虑我的文件夹中有3种不同类型的xmls: file1.xml file2.xml file3.xml 我有一个类代表上面的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[] […]

XmlDocument.Load失败,LoadXml工作:

在回答这个问题时 ,我遇到了一个我不理解的情况。 OP正在尝试从以下位置加载XML: http : //www.google.com/ig/api?weather = 12414&hl = 明显的解决方案是: string m_strFilePath = “http://www.google.com/ig/api?weather=12414&hl=it”; XmlDocument myXmlDocument = new XmlDocument(); myXmlDocument.Load(m_strFilePath); //Load NOT LoadXml 然而,这失败了 XmlException:给定编码中的字符无效。 第1行,第499位。 它似乎在Umidità上窒息。 OTOH,以下工作正常: var m_strFilePath = “http://www.google.com/ig/api?weather=12414&hl=it”; string xmlStr; using(var wc = new WebClient()) { xmlStr = wc.DownloadString(m_strFilePath); } var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlStr); 我为此感到困惑。 任何人都可以解释为什么前者失败,但后者工作正常吗? 值得注意的是,文档的xml声明省略了编码。