none
Xml Deserialize works with .Net 4.5.2, breaks with 4.6: xmlns was not expected RRS feed

  • Question

  • 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.  We eventually need to upgrade to 4.7


    Wednesday, June 12, 2019 7:49 PM

All replies

  • Seems to work well in some samples.

     

    If possible, prepare a short project, which includes classes and XML file and demonstrates the problem.

     

    Thursday, June 13, 2019 6:00 AM
  • Hi RunzWitScissors,

    Thank you for posting here.

    I test the code, but could not reproduce the error. Could you provide more details?

    The code sample which could reproduce the error would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 13, 2019 7:24 AM
    Moderator
  • Sorry for the delay.

    I've isolated the problem into a small console app.  How do I share that here?  Is there a means of uploading project files?

    -Jeff

    Friday, June 14, 2019 2:55 PM
  • Hi RunzWitScissors,

    You could upload on OneDrive.

    https://onedrive.live.com/

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 26, 2019 7:12 AM
    Moderator