Autor Beitrag
Ivy
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 228



BeitragVerfasst: Mo 25.10.10 11:08 
hallo,
ich möchte auch einen datensatz in meiner xml datei bearbeiten bzw aktualisieren. der fehler erscheint in der zeile ReplaceChild.
Zitat:
"Der zu entfernende Knoten ist diesem Knoten nicht untergeordnet."


Das ist mein quellcode:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
 XmlTextReader reader = new XmlTextReader(FILE_NAME);
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                reader.Close();
                XmlNode alterKnoten;
                XmlElement root = doc.DocumentElement;
                alterKnoten = root.SelectSingleNode("/Setting/IECBus/IECGerät[@ID='" + alterName + "']");
                XmlElement neuerKoten = doc.CreateElement("IECGerät");
                neuerKoten.SetAttribute("ID"this.cmbID.Text);
                neuerKoten.SetAttribute("Name", txtName.Text + "Adresse", txtAdresse.Text);
                root.ReplaceChild(neuerKoten, alterKnoten);
                doc.Save(FILE_NAME);


meine xml datei sieht so aus:

ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
<Setting>
  <IECBus>
    <IECGerät ID="0" Name="Start" Adresse="111">
    </IECGerät>
</IECBus>
</Setting>


gruß
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mo 25.10.10 11:23 
Da es kein direktes Kind der Wurzel ist, stimmt das doch auch so, oder? Du solltest wohl lieber an alterKnoten.ParentNode ansetzen. Die neuere, zu bevorzugende XElement-API kennt übrigens direkt eine Methode ReplaceWith.

PS: Warum nicht einfach die Attribute des alten Knoten überschreiben?

_________________
>λ=
Ivy Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 228



BeitragVerfasst: Mo 25.10.10 13:35 
user profile iconKha hat folgendes geschrieben Zum zitierten Posting springen:

PS: Warum nicht einfach die Attribute des alten Knoten überschreiben?


ja würde auch gehen xD

ausblenden C#-Quelltext
1:
neuerKoten.ParentNode.ReplaceWith("ID"this.cmbID.Text);					


funktioniert nicht...
Trashkid2000
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 561
Erhaltene Danke: 137



BeitragVerfasst: Mo 25.10.10 20:30 
Hi,
Du kannst auch direkt die Attribute des Knotens einen anderen Wert verpassen.
Denke, so meint es auch user profile iconKha
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
XmlNode node = doc.DocumentElement.SelectSingleNode("/Setting/IECBus/IECGerät[@ID='" + "0" + "']");
if (node != null)
{
  if (node.Attributes["ID"] != null &&
      node.Attributes["Name"] != null &&
      node.Attributes["Adresse"] != null)
  {
    node.Attributes["ID"].Value = "1";
    node.Attributes["Name"].Value = "xxx";
    node.Attributes["Adresse"].Value = "yyyy";
    //speichern
  }
}

//edit kurze Anmerkung gemacht

LG, Marko
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Di 26.10.10 19:10 
user profile iconIvy hat folgendes geschrieben Zum zitierten Posting springen:
funktioniert nicht...
Merke: XmlElement != XElement :P .

_________________
>λ=