Hi,

Im new to C# and need help with my program that im writing i need to let it read an XML in the first window and when i press add program a second window opens (both windows i have) i want it to write the XML so something like this (as you click on add) would come out.

<?xml version="1.0" encoding="utf-8"?>

<prog>

	<id=1>
		<name>Dreamweaver</name>
		<path>C:\Program Files\Adobe\Adobe Dreamweaver CS4\Dreamweaver.exe</path>
	</id>
	
	<id=2>
		<name>Photoshop</name>
		<path>C:\Program Files\Adobe\Adobe Photoshop CS4\Photoshop.exe</path>
	</id>
	
</prog>

then when the XML is adjusted that a button is made with on click it would do

Process.Start(@"D:\Program Files\Adobe\Adobe Dreamweaver CS4\Dreamweaver.exe");

If someone could help me on how to do this cause i already looked at the read & write tutorials on the web but cant get a real good grip on how to do it.

thx in advance

Dani AI

Generated

As outlined, the simplest pattern is: main form reads a predefined XML, creates one button per program entry, and the Add form writes a new entry into that XML. The example XML in the thread is malformed (tags like <id=1> are not valid). Use a clean, well-formed structure (example below) so LINQ to XML or XmlDocument can read and write it reliably.

<programs>
  <program id="1">
    <name>Dreamweaver</name>
    <path>C:\Path\To\Dreamweaver.exe</path>
  </program>
  <!-- more <program> elements -->
</programs>

Writing an entry from the Add form can be done with XDocument: load (or create) the document, compute the next id, append a new <program> element, then save. Example pattern:

void AddProgram(string xmlFile, string name, string exePath)
{
    var doc = File.Exists(xmlFile) ? XDocument.Load(xmlFile)
               : new XDocument(new XElement("programs"));
    var root = doc.Element("programs");
    int nextId = root.Elements("program")
                     .Select(x => (int?)x.Attribute("id") ?? 0)
                     .DefaultIfEmpty(0).Max() + 1;
    root.Add(new XElement("program",
                new XAttribute("id", nextId),
                new XElement("name", name),
                new XElement("path", exePath)));
    doc.Save(xmlFile);
}

On the main form, load the XML and create Buttons dynamically (store the executable path in Button.Tag). After the Add form closes with success, reload the XML to refresh buttons. Example pattern:

void LoadButtons(string xmlFile)
{
    var doc = XDocument.Load(xmlFile);
    flowLayoutPanel1.Controls.Clear();
    foreach (var p in doc.Descendants("program"))
    {
        string name = (string)p.Element("name") ?? "Unnamed";
        string path = (string)p.Element("path") ?? "";
        var btn = new Button { Text = name, Tag = path, AutoSize = true };
        btn.Click += (s,e) => Process.Start((string)((Button)s).Tag);
        flowLayoutPanel1.Controls.Add(btn);
    }
}

Practical cautions: validate exePath with File.Exists before saving; handle exceptions around load/save (permissions, file locks); save the XML to a writable location (writing into Program Files requires elevation); refresh the main form after the Add form closes (DialogResult or an event); and marshal UI updates onto the UI thread if needed. This approach keeps the XML simple, editable, and the UI logic straightforward for launching programs.

Recommended Answers

All 2 Replies

I'm afraid I don't understand what you're asking. What I think you're asking is:

When the program opens up the main form will let you select an XML file and open it. You want a list of the programs in the XML file to appear on the main form after you open up the XML file with a "Run" button next to each program that will run Process.Start().

Next you want a button on the main form to "Add" a new program where it will let you select a file and give a user typed name for it. After they add the program it will add the information to the XML file and save it.

Let me know if this is the intended design

When the program opens up the main form will let you select an XML file and open it.

No the XML file is predefined it nsacfg.xml

You want a list of the programs in the XML file to appear on the main form after you open up the XML file with a "Run" button next to each program that will run Process.Start().

not realy i want to let the app to read the XML Tags i have and use it to change the text that is in the button and the attach the path to the Process.Start() so when you click on the button it would start the program

Next you want a button on the main form to "Add" a new program where it will let you select a file and give a user typed name for it. After they add the program it will add the information to the XML file and save it.

Yes that is correct but this button already exists and i can go to the second window all the information i want is how to let it write to to XML from the second window with the correct TAG's, then let the first window read the XML and edits the button so that instead of EXAMPLE: the text is app it would read the TAG name and rename the button to that TAG and edit Process.Start() to Process.Start(@"D:\Program Files\Adobe\Adobe Dreamweaver CS4\Dreamweaver.exe"); for example

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.