// Remove element with ID of 1
var userIds = from user in document.Descendants("Id")
where user.Value == "1"
select user;
userIds.Remove();
SaveAndDisplay(document);
// Add element back
var newElement = new XElement("Id", "0",
new XElement("Balance", "3000"));
document.Add(newElement);
SaveAndDisplay(document);
Il blocco posteriore di elemento aggiuntivo è il problema. Come quando si arriva all'aggiunta:Come si aggiunge un XElement a un documento, evitando l'errore "documento strutturato in modo errato"?
Questa operazione creerebbe un documento strutturato in modo errato .
Che stupido errore sto facendo?
Edit:
Sì, stavo leggendo come XDocument
, non XElement
. Qualche consiglio su quando preferire uno o l'altro?
Una nota a parte, il vostro 'oggetto newElement' memorizza il nodo' Balance' come figlio di 'Id'. Questo non corrisponde alla struttura XML prevista. –
Hai ragione, ma puoi ignorare la struttura xml, ho pensato che fosse necessario ma posso letteralmente aggiungere qualcosa. – Finglas