C
C#13mo ago
Lucho

Can someone explain to me how this works

public bool EliminarClienteXML(BE_Cliente cliente)
{
acceso = new DatosXML();
XDocument docxml = acceso.LeerXML("Clientes.xml");

XElement elemento = docxml.Descendants("Cliente").FirstOrDefault(x => Convert.ToInt32(x.Element("DNI").Value) == cliente.dni);

elemento.Remove();
docxml.Save("Clientes.xml");
return true;
}
public bool EliminarClienteXML(BE_Cliente cliente)
{
acceso = new DatosXML();
XDocument docxml = acceso.LeerXML("Clientes.xml");

XElement elemento = docxml.Descendants("Cliente").FirstOrDefault(x => Convert.ToInt32(x.Element("DNI").Value) == cliente.dni);

elemento.Remove();
docxml.Save("Clientes.xml");
return true;
}
I dont understand how this line works:
XElement elemento = docxml.Descendants("Cliente").FirstOrDefault(x => Convert.ToInt32(x.Element("DNI").Value) == cliente.dni); This is how the xml file looks like:
<?xml version="1.0" encoding="utf-8"?>
<Clientes>
<Cliente>
<Nombre>Martin</Nombre>
<Apellido>Rodriguez</Apellido>
<DNI>45868900</DNI>
</Cliente>
</Clientes>
<?xml version="1.0" encoding="utf-8"?>
<Clientes>
<Cliente>
<Nombre>Martin</Nombre>
<Apellido>Rodriguez</Apellido>
<DNI>45868900</DNI>
</Cliente>
</Clientes>
15 Replies
Jimmacle
Jimmacle13mo ago
it's getting the first descendant named "Cliente" where the integer value of the "DNI" element equals whatever is in cliente.dni so if cliente.dni was 45868900 the XElement would contain this portion of the xml
<Cliente>
<Nombre>Martin</Nombre>
<Apellido>Rodriguez</Apellido>
<DNI>45868900</DNI>
</Cliente>
<Cliente>
<Nombre>Martin</Nombre>
<Apellido>Rodriguez</Apellido>
<DNI>45868900</DNI>
</Cliente>
Lucho
LuchoOP13mo ago
so descendant takes all the elements? inside Cliente?
Jimmacle
Jimmacle13mo ago
it gives you every sub-element in the tree, so it will give you Cliente, Nombre, Apellido, etc which honestly seems inefficient for what it's being used for, based on your data you should just be checking direct children of Clientes
Lucho
LuchoOP13mo ago
how and how does this work
Jimmacle
Jimmacle13mo ago
use Elements() instead of Descendants() iirc
Lucho
LuchoOP13mo ago
FirstOrDefault(x => Convert.ToInt32(x.Element("DNI").Value) == cliente.dni); whats x
Jimmacle
Jimmacle13mo ago
FirstOrDefault is LINQ, which is part of a collection of functions that help you filter/etc sets of data so here x => Convert.ToInt32(x.Element("DNI").Value) == cliente.dni is an anonymous function (or lambda) that takes in an x (the type is inferred from the type of the collection) and returns a bool it calls that function for every element in the collection provided by docxml.Descendants("Cliente") and returns the first element in the collection where that function returns true
Lucho
LuchoOP13mo ago
hmm ok so i dont need to declare the variable x right? its just part of the syntax
Jimmacle
Jimmacle13mo ago
it is being declared as part of the lambda definition that part of the code is the same as if you wrote a method like
bool MyMethod(XElement x)
{
return Convert.ToInt32(x.Element("DNI").Value) == cliente.dni;
}
bool MyMethod(XElement x)
{
return Convert.ToInt32(x.Element("DNI").Value) == cliente.dni;
}
with some extra magic to actually get the value of cliente.dni in the method (called a capture)
Lucho
LuchoOP13mo ago
hmm alright i think im getting it ty one last question tho if i remove the FirstOrDefault thing does it delete all the descendants from clients with the cliente.dni ?
Jimmacle
Jimmacle13mo ago
it won't compile XElement elemento can only hold one element, docxml.Descendants("Cliente") is a collection of multiple elements
Lucho
LuchoOP13mo ago
that makes sense can i use like a foreach(xElement element in docxml.Descendants("Cliente"){} or something like that? or how do i get all the Cliente with the same dni
Jimmacle
Jimmacle13mo ago
you could do that, keep in mind it will give you every element at any level so if you have Cliente elements nested inside each other you'll get all of them
Lucho
LuchoOP13mo ago
u mean id be getting "Nombre" and "Apellido" as elements as well?
Jimmacle
Jimmacle13mo ago
no because you're filtering descendants by name
Want results from more Discord servers?
Add your server