3
I’m making a mistake tormenting myself.
Basically I am saving all equipment settings (RF Readers) in an XML file (Configs.xml). My goal is to retrieve the first element of all settings that have a "set" value (other than "") in the new attributeLote. If the recovered element is different from null I proceed with the code within the IF, otherwise I will release an exception. However, the code below gives the following error when I am trying to retrieve the element(third line): Object Reference not defined for an instance of an object.
Can anyone tell me what I’m doing wrong? Thanks!
XElement xml = XElement.Load("Configs.xml");
XElement x = null;
x = xml.Elements().Where(p => !p.Attribute("novoLote").Value.Equals("")).FirstOrDefault();
if (x != null)
{
...
}
else
{
throw new ReaderNaoConfiguradoParaFuncaoException("Nenhum Reader está configurado para essa função.");
}
Hello Gypsy, unfortunately did not work the two options you talked about. Both give the same error reported in my question. :(
– João Vitor Araújo
Put a breakpoint on the line and check which of the elements is null.
– Leonel Sanches da Silva
Only x is null.
– João Vitor Araújo
O valor armazenado em xml é o seguinte:

<configs>
 <reader port="23" ip="192.168.8.3" login="alien" senha="password" antennaSequence="0,1" power="0" tagListFormat="Custom" customFormat="TagID: ${TAGIDW} TX: ${TX} SPEED: ${SPEED}" type="alienConfig" speedFilter="0.25, -0.25" autoMode="OFF" mac="00:1B:5F:00:4E:9F" newLote="1" contamination="" changeFase="" discard="" /> </configs>
– João Vitor Araújo
xml.Elements().First().Attribute("novoLote")
null return?– Leonel Sanches da Silva
Não, Ele devolve isso para x:

<reader port="23" ip="192.168.8.3" login="alien" senha="password" antennaSequence="0,1" power="0" tagListFormat="Custom" customFormat="TagID: ${TAGIDW} TX: ${TX} SPEED: ${SPEED}" type="alienConfig" speedFilter="0.25, -0.25" autoMode="OFF" mac="00:1B:5F:00:4E:9F" newLote="1" contamination="" changeFase="" discard="" />
– João Vitor Araújo
See that my question was another. My doubt is what is being null, not what is returned to x, so much so that I asked for another test in the code, as I put it above. Can you take this test, please?
– Leonel Sanches da Silva
I did so Gypsy: Xattribute a = xml. Elements(). First(). Attribute("newLote"); E returned: {newLote="1"}
– João Vitor Araújo
Weird. It has something null even, but I can’t tell you what it is, since the test returned an element.
– Leonel Sanches da Silva
Hello Gypsy, I managed to solve it. Your code helped a lot. It was a small detail that I changed and worked here. Follow the resolution: x = xml.Elements(). Firstordefault(p => p.Attribute("newLote").Value != null && !p.Attribute("newLote").Value.Equals("")); I added . Value in comparison to see if it was different from null.
– João Vitor Araújo
Thank you! I updated the reply.
– Leonel Sanches da Silva