我们有一个C#项目,该项目反序列化大型XML文件,并且与.Net 4.5.2一起正常工作。
将项目升级到.Net 4.6后,我们在xmlserializer.Deserialize调用中收到以下异常:
System.InvalidOperationException
HResult=0x80131509
Message=There is an error in XML document (1, 2).
Inner Exception 1:
InvalidOperationException: <AccountTransferRequest xmlns='http://at.dsh.cms.gov/exchange/1.0'> was not expected.
C#代码:
string filename = @"C:\CARES_TFS\FIPS140\Cares\Cares.Test\TestData\FFM Sync\FFMStateAidCat38.xml";
var xmlReader = new XmlTextReader(new FileStream(filename, FileMode.Open));
var xmlserializer = new XmlSerializer(typeof(AccountTransferRequestPayloadType));
var obj = xmlserializer.Deserialize(xmlReader) as AccountTransferRequestPayloadType;
我们反序列化到的C#类是自动生成的,如下所示:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1087.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://at.dsh.cms.gov/extension/1.0")]
[System.Xml.Serialization.XmlRoot("AccountTransferRequest", IsNullable = false, Namespace = "http://at.dsh.cms.gov/exchange/1.0")]
public partial class AccountTransferRequestPayloadType : ComplexObjectType { ... }
以下是xml文件的第一行:
<exch:AccountTransferRequest
ext:atVersionText="2.4"
xsi:schemaLocation="http://at.dsh.cms.gov/exchange/1.0 ../XSD/XMLschemas/constraint/exchange/ExchangeModel.xsd"
xmlns:exch="http://at.dsh.cms.gov/exchange/1.0"
xmlns:ext="http://at.dsh.cms.gov/extension/1.0"
xmlns:hix-core="http://hix.cms.gov/0.1/hix-core"
xmlns:hix-ee="http://hix.cms.gov/0.1/hix-ee"
xmlns:hix-pm="http://hix.cms.gov/0.1/hix-pm"
xmlns:nc="http://niem.gov/niem/niem-core/2.0"
xmlns:s="http://niem.gov/niem/structures/2.0"
xmlns:scr="http://niem.gov/niem/domains/screening/2.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
我们尝试过的事情:
1)添加:
<system.xml.serialization>
<xmlSerializer useLegacySerializerGeneration="true" />
</system.xml.serialization>
到app.config
2)删除XML文件中的名称空间和类属性
3)删除对4.6版本的System.Xml.dll的引用,并添加对System.Xml.dll的4.5.2版本的引用(而将项目的其余部分保留在.Net 4.6中)。这实际上是可行的,但不是我们想要的解决方案