Problem removing or modifying a node in XML

3

I have a project in which I manage an XML file with C # UWP.

The problem is that when I delete or modify a node in the XML document, the space of that node is not deleted and it is under the root, causing me to get an error.

I give an example:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Usuario>
   <Nombre>
      <Valor>Antonio</Valor>
   </Nombre>
   <Contrasena>
      <Valor>Antonio1989</Valor>
   </Contrasena>
   <Apellido>
      <Valor>Pérez</Valor>
   </Apellido>
</Usuario>

This is how it would look like without any changes, but when I delete the "Password" node, for example, it looks something like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Usuario>
   <Nombre>
      <Valor>Antonio</Valor>
   </Nombre>
   <Apellido>
      <Valor>Pérez</Valor>
   </Apellido>
</Usuario>o>
      <Valor>Pérez</Valor>
   </Apellido>
</Usuario>

The code fragment I use in C # is the following (the "path" is a IsolatedStorageFileStream ):

public void EliminarNodo(string nodo)
{
   XmlDocument Documento = new XmlDocument();
   Documento.Load(path);

   XmlElement Root = Documento.DocumentElement;
   XmlNode NodoEliminar = Root.GetElementsByTagName(nodo)[0];
   Root.RemoveChild(NodoEliminar);

   Documento.Save(AlmacenDatos.Stream(Ruta));
}
    
asked by MTKt 13.07.2017 в 13:35
source

1 answer

1

You can delete the node through a XElement :

XElement XML = XElement.Load(path);
EliminaNodos(XML.Element("Usuario").Element("Contraseña"));

Method that removes the node:

public void EliminaNodo(XElement nodoAEliminar)
{
    nodoAEliminar.RemoveNodes();
}

Similarly, to eliminate the node, you can use the Remove(); and / or RemoveAll();

methods     
answered by 13.07.2017 в 17:12