XML使用c#从xsd反序列化iso 20022 pain.001.001.03

时间:2016-07-25 20:02:35

标签: c# xml visual-studio deserialization xsd.exe

我想从xml文件中获取一个对象。在我的例子中,我使用iso 2002 pain.001.001.03

ISO 200022 pain.001.001.03

我已从

下载了架构

pain.001.001.03.xsd

中的xml文件

pain.001.001.03.xml

我已使用此工具

对xsd验证了我的xml

Validate XML

我使用xsd

生成了一个类

enter image description here

我正在使用下面的代码来反序列化

   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);

代码返回null值,但我的xml具有值

enter image description here

1 个答案:

答案 0 :(得分: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>

根元素是Document,而不是CstmrCdtTrfInitn

var serializer = new XmlSerializer(typeof(Document));
using (var file = File.OpenRead(path))
{
    var document = (Document)serializer.Deserialize(file);
    var transfer = document.CstmrCdtTrfInitn;
}