C# Xml Edit?

3 Antworten

Lesen kannst du den dritten Knoten folgendermaßen:

xmlDoc.Load(filePath);

XmlNodeList nodes = xmlDoc.SelectNodes("//Core/add");
string key = nodes[2].Attributes["key"].Value;
string value = nodes[2].Attributes["value"].Value;

Schreiben kannst du genauso:

xmlDoc.Load(filePath);

XmlNodeList nodes = xmlDoc.SelectNodes("//Core/add");
nodes[2].Attributes["key"].Value = "Wert";
nodes[2].Attributes["value"].Value = "Wert";

xmlDoc.Save(filePath);

Du kannst das XML wunderbar in ein eigenes Dictionary<string, bool> schreiben. Ich empfehle eine Load und Save Funktion dafür zu erstellen.

LG Knom

Woher ich das weiß:Studium / Ausbildung – Softwareentwickler mit 10 Jahren Berufserfahrung 💾

In Verbindung mit meiner anderen Antwort habe ich mal ein praktisches Beispiel erstellt. Die Klasse MyXMLReader ließt das XML in ein Dictionary ein wenn du ein Objekt von ihr erstellst. Du kannst nach Werten suchen, sie ändern und am Ende speicher. LG Knom

class MyXMLReader 
{
  Dictionary<string, int> xmlcontents = new Dictionary<string, int>();
  public MyXMLReader(string filePath)
  {
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(filePath);
    XmlNodeList nodes = xmlDoc.SelectNodes("//Core/add");
    foreach(XmlNode node in nodes)
    {
      string key = node.Attributes["key"].Value;
      bool value = Convert.ToBoolean(node.Attributes["value"].Value);
      xmlcontents.Add(key, value);
    }
  }

  public bool GetValue(string key)
  {
    return xmlcontents[key];
  }

  public void ChangeValue(string key, bool value)
  {
    xmlcontents[key] = value;
  }

  public void SaveMyXML(string filePath)
  {
    XmlDocument xmlDoc = new XmlDocument();
    XmlElement root = xmlDoc.CreateElement("Core");
    foreach(Dictionary<string, int> pair in xmlcontents)
    {
      XmlElement add = xmlDoc.CreateElement("add");
      XmlAttribute key = xmlDoc.CreateAttribute("key");
      XmlAttribute value = xmlDoc.CreateAttribute("value");
      key.Value = pair.Key.ToString();
      value.Value = pair.Value.ToString();
      add.Attributes.Add(key);
      add.Attributes.Add(value);
      root.AppendChild(add);
    }
    xmlDoc.AppendChild(root);
    xmlDoc.Save(filePath);
  }
}

Woher ich das weiß:Studium / Ausbildung – Softwareentwickler mit 10 Jahren Berufserfahrung 💾

Das Snippet sollte dir weiterhelfen:

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(xmlFile);

XmlNode node = xmlDoc.SelectSingleNode("Root/Node/Element");
node.Attributes[0].Value = newValue;

xmlDoc.Save(xmlFile);

Quelle:https://im-coder.com/so-aendern-sie-das-xml-attribut.html

Woher ich das weiß:Berufserfahrung – Anwendungsentwickler (vorwiegend .NET)

Biggi1234567 
Beitragsersteller
 16.10.2019, 12:39

Das hab ich auch schon Probiert, habs von StackOver ^^

Leider funktioniert das nicht .....

Ich nehme an das [0] in der Zeile:

node.Attributes[0].Value = newValue;

gibt an welches der 3 Key´s ich haben möchte oder ?

[0] = Key1
[1] = Key2
[2] = Key3

Oder ?

0
BlazedTowel  16.10.2019, 12:41
@Biggi1234567

Du könntest im Debug-Modus mal reinschauen. Aber es müssten die Attribute sein. Also key und value, wenn mich nicht alles täuscht. Key1, 2 und 3 müssten glaub ich Elemente sein, aber ich bin mir grad nicht sicher^^

0
BlazedTowel  16.10.2019, 12:53
@Biggi1234567

Der Code funktioniert bei mir:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(tbContent.Text);

XmlNode node = xmlDoc.SelectSingleNode("Core");

node.ChildNodes[2].Attributes[1].Value = "true";
StringWriter writer = new StringWriter();
xmlDoc.Save(writer);
tbContent.Text = writer.ToString();
0
Biggi1234567 
Beitragsersteller
 18.10.2019, 14:26
@guschteusz

Gar keine es scheint zu funktionieren, aber tut trozdem nichts ^^
Hab inzwischen alles ausprobiert aber leider ohne erfolg

0
Biggi1234567 
Beitragsersteller
 18.10.2019, 14:31
@guschteusz

Sry vergiss was ich geschrieben habe....
Falscher Beitrag ^^

0