Element Xml nie jest obecny, a lista null i Object reference nie są ustawione na wystąpienie obiektu

Odzyskuję dane z pola xml, istnieją węzły, które mogą nie istnieć (ponieważ plik xml jest generowany dynamicznie). problem polega na tym, że gdy szukam węzła, który nie jest obecny w pliku, zwraca listę zerową i ten wyjątek: „d Odwołanie do obiektu nie jest ustawione na wystąpienie obiektu”. Oto kod:

public static List<Transaction> getXmlTransactions(XElement n)
        {


            var transactions = n.Elements("Transaktion").Select(p => new Transaction()
                {
                    TransID = p.Element("TransID") != null ? p.Element("TransID").Value : String.Empty,
                    TypeTransaction = p.Element("TransArt") != null ? p.Element("TransArt").Value : String.Empty,
                    DateEntree = p.Element("BuchDat") != null ? p.Element("BuchDat").Value : String.Empty,
                    Montant = p.Element("BetragWAE") != null ? p.Element("BetragWAE").Value : String.Empty,
                    Devise = p.Element("BuchDat") != null ? p.Element("Waehrung").Value : String.Empty,
                    // BanqueCorespondante = p.Element("BuchDat") != null ? p.Element("Waehrung").Value : String.Empty,   Dans le compte
                    Pays = p.Element("GegenLandText") != null ? p.Element("GegenLandText").Value : String.Empty,
                    AbreviationPays = p.Element("GegenLand") != null ? p.Element("GegenLand").Value : String.Empty,
                    autresinfo = p.Element("Kommentar") != null ? p.Element("Kommentar").Value : String.Empty
                }).ToList();


                return transactions;



        } 

 public static List<Compte> getXmlComptes(XElement n)
        {


            var comptes = n.Elements("Konto").Select(p => new Compte()
            {
                NumCompte = p.Element("KtoNr") != null ? p.Element("KtoNr").Value : String.Empty,
                typeCompte = p.Element("KontoArt") != null ? p.Element("KontoArt").Value : String.Empty,
                DateOuverture = p.Element("KtoOeff") != null ? p.Element("KtoOeff").Value : String.Empty,
                IBAN = p.Element("IBAN") != null ? p.Element("IBAN").Value : String.Empty,
                Devise = p.Element("Waehrung") != null ? p.Element("Waehrung").Value : String.Empty,
                CommentairesCompte = p.Element("Kommentar") != null ? p.Element("Kommentar").Value : String.Empty,
                Trans = getXmlTransactions(p)
            }).ToList();


                return comptes;

        }

questionAnswers(1)

yourAnswerToTheQuestion