Eventuali duplicati:
How to remove an XmlNode from XmlNodeListCome eliminare nodo da file XML utilizzando C#
Ciao, come posso eliminare un insieme di nodi da un file XML.? Ecco uno snippet di codice.
string path = @"C:\Documents and Settings\e454935\Desktop\NUnitSettings.xml";
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
xmldoc.Load(fs);
fs.Close();
xmldoc.DocumentElement.RemoveChild(xmldoc.DocumentElement.ChildNodes[1]);
FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
xmldoc.Save(WRITER);
WRITER.Close();
Ho provato il seguente codice semplicemente per eliminare un nodo e ottenuto "Oggetto non impostato a un'istanza di un oggetto." a
xmldoc.DocumentElement.RemoveChild(xmldoc.DocumentElement.ChildNodes[1]);
Ecco un file XML di esempio,
<?xml version="1.0"?>
<Xml1>
<Settings>
<Setting name="DisplayFormat" value="Full" />
<Setting name="File1" value="a" />
<Setting name="File1" value="b" />
<Setting name="File1" value="c" />
<Setting name="File1" value="d" />
</Settings>
</Xml1>
In realtà da questo file voglio eliminare i Quattro FILE1 nodi che ha i valori "a, b, c, d" e poi voglio aggiungere un nodo,
<Setting name="File1" value="e" />
Come posso fare questo.?
Questa risposta è già stata qui http://stackoverflow.com/questions/875136 e probabilmente due dozzine di volte in altre domande. Votato per chiudere come un dupe. – Tomalak