Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[C#] XML Knoten erstellen

Empfohlene Antworten

Veröffentlicht

hey,

so, das letzte problem habe ich alleien gelöst bekommen

:bimei

nun, kann mir jemand sagen, wie ich die ausgewählte eigenschaft einer combobox rausfinde und daraufhin was ausführe?

mein code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Net;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Text = "Change LogLevel";
this.WindowState = FormWindowState.Normal;
}

private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();

List<string> files = this.GetFiles(@"C:\Temp", new List<string>());
string[] lines = new string[files.Count];

int i = 0;
foreach (string file in files)
{
listBox1.Items.Add(file);
i++;
}

}
private List<string> GetFiles(string folderPath, List<string> fileList)
{
if (!Directory.Exists(folderPath)) return fileList;
string[] dirs = Directory.GetDirectories(folderPath);
foreach (string subDir in dirs)
GetFiles(subDir, fileList);

string[] files = Directory.GetFiles(folderPath, "*.config");
foreach (string file in files)
if (!fileList.Contains(file))
fileList.Add(file);

return fileList;
}


private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();

MessageBox.Show("Directory-List cleared!", "Success!");
}


private void button4_Click(object sender, EventArgs e)
{
StreamReader reader = new StreamReader(listBox1.SelectedItem.ToString());
string file = listBox1.SelectedItem.ToString();
XmlDocument doc = new XmlDocument();

doc.LoadXml(reader.ReadToEnd());
reader.Close();

XmlNodeList nodelist = doc.SelectNodes("/configuration/loggingConfiguration/logFilters/add/categoryFilters/add");
XmlNodeList parent = doc.SelectNodes("/configuration/loggingConfiguration/logFilters/add/categoryFilters");
XmlNode catFilterNode = doc.SelectSingleNode("/configuration/loggingConfiguration/logFilters/add/categoryFilters");

XmlNodeList addNodesList = catFilterNode.SelectNodes("add");
foreach (XmlNode node in addNodesList)
{
catFilterNode.RemoveChild(node);
}

// neuen knoten erstellen

StreamWriter writer = new StreamWriter(file);
string[] xml = doc.InnerXml.Split('>');

//writer.WriteLine(xml[0] + ">");

foreach (string sfda in xml)
{
writer.WriteLine(sfda + ">");
}
writer.Flush();
writer.Close();

MessageBox.Show("LogLevel set to {0}", "Success!");
}

private void label1_Click(object sender, EventArgs e)
{
label1.Text = "Set LogLevel";
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

}
}
[/php]

wenn ich jetzt in der combobox das erste item ("debug") ausgewählt habe, soll mir der xmlwriter in die vorher geleerten Category filters das hier schreiben:

[php]
<add name="Debug" />

wenn ich das zweite item ausgewählt habe ("info"), soll er mir


<add name="Info" />
[/php]

reinschreiben.

[php]
<categoryFilters>
<add name="Debug" />
<add name="Info" />
<add name="Warning" />
<add name="Error" />
<add name="Fatal" />
</categoryFilters>

so siehts am anfang aus. daraus mache ich dann durch ne abfrage:


<categoryFilters>

</categoryFilters>
[/php]

und nun soll er mir halt jeh nach auswahl in der combobox ein item wieder reinschreiben.

danke und mfg

SeToY

Hallo SeToy,

ich hoffe ich habe dein Problem richtig verstanden.


private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

        {

XmlDocument doc = new XmlDocument();

string filename = "Document.xml";

doc.Load(filename);


XmlElement categoryFilters = (XmlElement)doc.SelectSingleNode("//categoryFilters"); //SelectSingleNode nimmt einen XPath-Ausdruck. Du kannst hier also auch den kompletten Pfad zum Knoten angeben: /root/knoten1/knoten2/categoryFilters


XmlElement addCategory = (XmlElement)categoryFilters.AppendChild(doc.CreateElement("add"));

addCategory.SetAttribute("name", combobox1.SelectedValue()); //SetAttribute nimmt zwei Werte vom Typ string. Ich weiß jetzt nicht genau was SelectedValue() zurückgibt. Gegebenenfalls muss es in einen String umgewandelt werden


doc.Save(filename);


        }


Dabei darf der Knoten categoryFilters nur einmal in deinem XmlDokument auftauchen. Es ist auch besser, dass Dokument nur einmal zu laden, und nicht bei jedem Event-Aufruf.

Grüße

Bastie

Bearbeitet von riote

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.