0

When I try to read this part of XML, it returns nullreference when I go through the variable: The error is in the attached image + base {"Referencia a objeto no establecida como instancia de un objeto."} System.SystemException {System.NullReferenceException}

Can you help me! xml part:

<?xml version="1.0" encoding="UTF-8"?>
<FACTURA>
<CONCEPTO>
  <CUSTOMER_TRX_ID>300000001703596</CUSTOMER_TRX_ID>
  <CUSTOMER_TRX_LINE_ID>300000001703597</CUSTOMER_TRX_LINE_ID>
  <DESCRIPTION>Salsa Extra Virgen</DESCRIPTION>
  <CANTIDAD>100</CANTIDAD>
  <VALOR_UNITARIO>10</VALOR_UNITARIO>
  <IMPORTE>1000</IMPORTE>
  <CODIGO>10009</CODIGO>
  <UNIDAD>C/4G</UNIDAD>
    <IMPUESTOCONCEPTO>
      <IMPORTEIMPUESTO>160</IMPORTEIMPUESTO>
      <DESCRIPCIONIMPUESTO>AR_16</DESCRIPCIONIMPUESTO>
      <LINK_TO_CUST_TRX_LINE_ID>300000001703597</LINK_TO_CUST_TRX_LINE_ID>
      <TASAOCUOTAIMPUESTO>16</TASAOCUOTAIMPUESTO>
      <NOMBREIMPUESTO>002</NOMBREIMPUESTO>
      <BASEIMPUESTO>1000</BASEIMPUESTO>
      <TIPOIMPUESTO>T</TIPOIMPUESTO>
      <TIPOFACTOR>TASA</TIPOFACTOR>
    </IMPUESTOCONCEPTO>
</CONCEPTO>
</FACTURA>

Code:

Error nullReference

var concepto = from c in xdoc.Descendants("CONCEPTO")//Investigar por que muestra null
                   select new
                   {
                       CUSTOMER_TRX_ID = c.Element("CUSTOMER_TRX_ID").Value,
                       CUSTOMER_TRX_LINE_ID = c.Element("CUSTOMER_TRX_LINE_ID").Value,
                       DESCRIPTION = c.Element("DESCRIPTION").Value,
                       VALOR_UNITARIO = c.Element("VALOR_UNITARIO").Value,
                       IMPORTE = c.Element("IMPORTE").Value,
                       CLAVEPRODSERVSAT = c.Element("CLAVEPRODSERVSAT").Value,
                       CODIGO = c.Element("CODIGO").Value,
                       UNIDAD = c.Element("UNIDAD").Value,
                       CANTIDAD = c.Element("CANTIDAD").Value,
                       CLAVEUNIDADSAT = c.Element("CLAVEUNIDADSAT").Value,
                   };
    foreach (var c in concepto)
    {
        conE = new ConceptoEntity();
        conE.CUSTOMER_TRX_ID = c.CUSTOMER_TRX_ID;
        conE.CUSTOMER_TRX_LINE_ID = c.CUSTOMER_TRX_LINE_ID;
        conE.DESCRIPTION = c.DESCRIPTION;
        conE.VALOR_UNITARIO = c.VALOR_UNITARIO;
        conE.IMPORTE = c.IMPORTE;
        conE.CANTIDAD = c.CANTIDAD;
        conE.CLAVEPRODSERVSAT = c.CLAVEPRODSERVSAT;
        conE.CODIGO = c.CODIGO;
        conE.UNIDAD = c.UNIDAD;
        //conE.CLAVEUNIDADSAT = c.CLAVEUNIDADSAT;
        //conE.IMPUESTOCONCEPTO = c.IMPUESTOCONCEPTO;
        lConcepto.Add(conE);
    }

Here is solution :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            List<ConceptoEntity> lConcepto = new List<ConceptoEntity>();

            XDocument xdoc = XDocument.Load(FILENAME);

            lConcepto = (from c in xdoc.Descendants("CONCEPTO")//Investigar por que muestra null
                           select new ConceptoEntity()
                           {
                               CUSTOMER_TRX_ID = c.Element("CUSTOMER_TRX_ID").Value,
                               CUSTOMER_TRX_LINE_ID = c.Element("CUSTOMER_TRX_LINE_ID").Value,
                               DESCRIPTION = c.Element("DESCRIPTION").Value,
                               VALOR_UNITARIO = c.Element("VALOR_UNITARIO").Value,
                               IMPORTE = c.Element("IMPORTE").Value,
                               //CLAVEPRODSERVSAT = c.Element("CLAVEPRODSERVSAT").Value,
                               CODIGO = c.Element("CODIGO").Value,
                               UNIDAD = c.Element("UNIDAD").Value,
                               CANTIDAD = c.Element("CANTIDAD").Value,
                               //CLAVEUNIDADSAT = c.Element("CLAVEUNIDADSAT").Value,
                           }).ToList();

        }

    }
    public class ConceptoEntity
    {
        public string CUSTOMER_TRX_ID { get; set; }
        public string CUSTOMER_TRX_LINE_ID { get; set; }
        public string DESCRIPTION { get; set; }
        public string VALOR_UNITARIO { get; set; }
        public string IMPORTE { get; set; }
        public string CANTIDAD { get; set; }
        public string CLAVEPRODSERVSAT { get; set; }
        public string CODIGO { get; set; }
        public string UNIDAD { get; set; }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • And you did call `xdoc = XDocument.Load("file.xml");` somewhere? – rene Aug 25 '17 at 21:58
  • 1
    Don't assume elements exist. Your example shows that you are missing elements. `c.Element("CLAVEPRODSERVSAT").Value` -- that doesn't exist in your example XML and you are now trying to call `Value` off of null. – TyCobb Aug 25 '17 at 22:00
  • Addition to @TyCobb, `CLAVEUNIDADSAT = c.Element("CLAVEUNIDADSAT")?.Value` – L.B Aug 25 '17 at 22:01
  • thank you for your help, this was my problem. – Andres Martinez Aug 25 '17 at 22:12
  • I also found issue by commenting out code until I found which tags were missing. I simplified the code a lot. See you posting above for changes. – jdweng Aug 25 '17 at 22:15

0 Answers0