Xml Deserialize работает с .Net 4.5.2, разрывы с 4.6: xmlns не ожидался - PullRequest
0 голосов
/ 12 июня 2019

У нас есть проект 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) Удалите ссылку на версию System.Xml.dll версии 4.6 и добавьте ссылку на версию System.Xml.dll версии 4.5.2 (оставив остальную часть проекта в .Net 4.6). Это на самом деле работало, но не решение, которое мы хотели бы

...