0

We have a C# project that deserializes large XML files and has been working fine with .Net 4.5.2.

As soon as we upgrading the project to .Net 4.6 we get the following exception on the xmlserializer.Deserialize call:

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# code:

    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;

The C# class we are deserializing into to is auto-generated, and looks like the following:

[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 { ... }

The following is the first line of the xml file:

<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">

Things we've tried:

1) Adding:

  <system.xml.serialization>
    <xmlSerializer useLegacySerializerGeneration="true" />
  </system.xml.serialization>

to the app.config

2) Removing the namespaces in the XML file and the class attributes

3) Removing the reference to the 4.6 version System.Xml.dll and adding a reference to the 4.5.2 version of System.Xml.dll (while leaving the rest of the project at .Net 4.6). THIS ACTUALLY WORKED, but not the solution we'd like

RunzWitScissors
  • 128
  • 1
  • 8
  • 2
    `XmlTextReader` has been deprecated for over a decade. From the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmltextreader): *Starting with the .NET Framework 2.0, we recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.* What happens if you switch to `XmlReader.Create()`? – dbc Jun 12 '19 at 04:49
  • https://stackoverflow.com/q/4884383/17034 – Hans Passant Jun 12 '19 at 05:06
  • @HansPassant: I did try that but still got the same error. – RunzWitScissors Jun 12 '19 at 14:11
  • Assuming that replacing `XmlTextReader` changes nothing, the only thing I can think is that your `AccountTransferRequestPayloadType` has somehow been built using `System.Xml.Serialization.XmlRootAttribute` from an inconsistent version of the DLL. Make sure you've rebuilt completely and are referencing consistent versions of `System.Xml.dll` across all projects. – dbc Jun 13 '19 at 18:03

0 Answers0