5

I want to get an object from an xml file. In my example I am using iso 2002 pain.001.001.03

ISO 200022 pain.001.001.03

I have downloaded the schema from

pain.001.001.03.xsd

and the xml file from

pain.001.001.03.xml

I have validated my xml against the xsd using this tool

Validate XML

I have generated a class using xsd

enter image description here

and I am using the code below in order to deserialize

   XmlSerializer ser = new XmlSerializer(typeof(CustomerCreditTransferInitiationV03),    new XmlRootAttribute                             
                     { 
                         ElementName = "Document",
                        Namespace = "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03",
                   });


           FileStream myFileStream = new FileStream("C:\\001.001.03\\pain.001.001.03.xml", FileMode.Open);

           CustomerCreditTransferInitiationV03 myObject = (CustomerCreditTransferInitiationV03) ser.Deserialize(myFileStream);

The code return null values but my xml has values

enter image description here

EduardoUstarez
  • 603
  • 11
  • 22

1 Answers1

5
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
    <CstmrCdtTrfInitn>

The root element is an Document, and not a CstmrCdtTrfInitn :

var serializer = new XmlSerializer(typeof(Document));
using (var file = File.OpenRead(path))
{
    var document = (Document)serializer.Deserialize(file);
    var transfer = document.CstmrCdtTrfInitn;
}
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • I have changed my code and It returns this message "Error en el documento XML (50, 7)" in the inner exception returns "{"La cadena de entrada no tiene el formato correcto."}". I am trying to fix that before I check yout answer as a correct one. – EduardoUstarez Jul 25 '16 at 21:00
  • @EduardoUstarez I didn't have any problem deserializing the document from the link you provided. I can try look into the problem if you provide the exactly class definitions, and the xml (if you are using a different one). – Xiaoy312 Jul 25 '16 at 21:12
  • It seems like a weird problem. The xml at line 50 is `ABC/4562/2009-09-08`. The exception indicated there is a format issue, however it is being deserialized into a `string` (`PaymentIdentification1.endToEndIdField`). – Xiaoy312 Jul 25 '16 at 21:15
  • Yes, it is weird but I am working on other xml and it works so thanks for your answer. – EduardoUstarez Jul 25 '16 at 21:40