简体   繁体   English

不存在路径的C#XMl反序列化

[英]C# XMl Deserialisation with non existing Paths

I've with xsd.exe created the class for my XML. 我已经使用xsd.exe为我的XML创建了类。 In the XML (SEPA camt.053) are multiply nested Nodes that have minOccurs = 0. 在XML(SEPA camt.053)中,具有minOccurs = 0的多重嵌套节点。

I navigate to the node like this: 我导航到这样的节点:

entry.NtryDtls[0].TxDtls[0].RltdPties.CdtrAcct.Id.Item

In this Path RltdPties, CdtrAcct and Id could be null. 在此Path RltdPties中,CdtrAcct和Id可以为空。

If RltdPties is null I get an Exception, because CdtrAcct doesn't exist. 如果RltdPties为null,则会得到异常,因为CdtrAcct不存在。

Is there an easy way without checking each node if it is null to get the Value of Item? 有没有一种简单的方法可以不检查每个节点是否为空以获取Item的值? If path doesn't exist it would be enough to get null (or something else) from the last node that I can check. 如果路径不存在,那么从我可以检查的最后一个节点获取null(或其他值)就足够了。

My idea is to surround this query with try/catch and if it throws an Exception I can set the Variable to an empty string or so. 我的想法是用try / catch包围此查询,如果引发异常,则可以将Variable设置为空字符串左右。

Quick answer is no. 快速答案是没有。 You have to check for a null reference before invoking methods or operators upon members. 在成员上调用方法或运算符之前,必须检查空引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM