XML元素不存在,并列出NULL和对象引用未设置为对象的实例



我正在从XML字段中退休数据,有些节点可能不存在(因为XML文件是动态生成的)。问题是当我寻找一个节点并且文件中不存在时,它会返回一个空列表,并且此例外:" d对象引用未设置为对象的实例"。这是代码:

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

您可以说,您可以说,您将null传递给getXmlComptes方法(或getXmlTransactions,如果被称为其他地方)。

我建议您投射元素:

public static List<Transaction> getXmlTransactions(XElement n)
{
   if (n == null)
       throw new ArgumentNullException("Konto is null");
    var transactions = from t in n.Elements("Transaktion")
                       select new Transaction {
                           TransID = (string)t.Element("TransID"),
                           TypeTransaction = (string)t.Element("TransArt"),
                           DateEntree = (string)t.Element("BuchDat"),
                           Montant = (string)t.Element("BetragWAE"),
                           Devise = (string)t.Element("BuchDat"),
                           Pays = (string)t.Element("GegenLandText"),
                           AbreviationPays = (string)t.Element("GegenLand"),
                           autresinfo = (string)t.Element("Kommentar")
                       };
    return transactions.ToList();
}

和第二种方法:

public static List<Compte> getXmlComptes(XElement n)
{
   if (n == null)
       throw new ArgumentNullException("Something is null");
    var comptes = from k in n.Elements("Konto")
                  select new Compte
                  {
                      NumCompte = (string)k.Element("KtoNr"),
                      typeCompte = (string)k.Element("KontoArt"),
                      DateOuverture = (string)k.Element("KtoOeff"),
                      IBAN = (string)k.Element("IBAN"),
                      Devise = (string)k.Element("Waehrung"),
                      CommentairesCompte = (string)k.Element("Kommentar"),
                      Trans = getXmlTransactions(k)
                  };
    return comptes.ToList();
}

运行此代码,您会看到您正在通过null。如果如果XML中缺少元素,则需要将空字符串分配给某些属性,请使用null-Coalescing Operator:

TypeTransaction = (string)t.Element("TransArt") ?? ""

,但我会将属性价值作为 null保留,以表明它不在xml中。

最新更新