2009-05-28 3 views
15

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.?

+3

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

risposta

6

Potrebbe essere più semplice utilizzare XPath per individuare i nodi che si desidera eliminare. This stackoverflow thread potrebbe darti qualche idea.

Nel tuo caso si trovano i quattro nodi che si desidera utilizzare questa espressione:

XmlDocument doc = new XmlDocument(); 
doc.Load(fileName); 
XmlNodeList nodes = doc.SelectNodes("//Setting[@name='File1']"); 
2

DocumentElement è il nodo radice del documento, pertanto childNodes[1] non esiste in tale documento. childNodes[0] è il <Impostazioni> nodo

+0

+1 per spiegare dove si trova l'errore. – Tomalak

23

È possibile utilizzare Linq to XML per fare questo:

XDocument doc = XDocument.Load("input.xml"); 
var q = from node in doc.Descendants("Setting") 
     let attr = node.Attribute("name") 
     where attr != null && attr.Value == "File1" 
     select node; 
q.ToList().ForEach(x => x.Remove()); 
doc.Save("output.xml"); 
6

Eliminazione di nodi da XML

  XmlDocument doc = new XmlDocument(); 
      doc.Load(path); 
      XmlNodeList nodes = doc.SelectNodes("//Setting[@name='File1']"); 
      for (int i = nodes.Count - 1; i >= 0; i--) 
      { 
       nodes[i].ParentNode.RemoveChild(nodes[i]); 
      } 
      doc.Save(path); 

Aggiunta attributo ai nodi in XML

XmlDocument originalXml = new XmlDocument(); 
    originalXml.Load(path); 
    XmlNode menu = originalXml.SelectSingleNode("//Settings"); 
    XmlNode newSub = originalXml.CreateNode(XmlNodeType.Element, "Setting", null); 
    XmlAttribute xa = originalXml.CreateAttribute("name"); 
    xa.Value = "qwerty"; 
    XmlAttribute xb = originalXml.CreateAttribute("value"); 
    xb.Value = "555"; 
    newSub.Attributes.Append(xa); 
    newSub.Attributes.Append(xb); 
    menu.AppendChild(newSub); 
    originalXml.Save(path); 
+0

Non sei sicuro del motivo per cui sei stato downvoted, ma +1 da me. That node.ParentNode.RemoveChild (node) è una linea pratica di codice da ricordare. –