C# Xml Edit?
Hallo,
wenn ich eine XML habe die so aussieht:
<Core>
<add key="Key1" value="false" />
<add key="Key2" value="false" />
<add key="Key3" value="false" />
</Core>
Wie kann ich nun auf auf das 3 Attribut zugreifen und den Wert Ändern ?
Sollte dann so aussehen:
<Core>
<add key="Key1" value="false" />
<add key="Key2" value="false" />
<add key="Key3" value="true" />
</Core>
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
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);
}
}
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
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^^
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();
Gar keine es scheint zu funktionieren, aber tut trozdem nichts ^^
Hab inzwischen alles ausprobiert aber leider ohne erfolg
Sry vergiss was ich geschrieben habe....
Falscher Beitrag ^^
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 ?