2

How can I create automatic GUI using XML file in C#? Should I write a parser for the file and define a sort of "protocol" for the file's structure, and after the parse - create the GUI controls manually (respective to the data in the files)?

Or is there a better way? Is there a tool, or a built-in code in the .NET environment which can do that for me automatically?

(I am currently working with win forms, but I am willing to consider any other technology - as long as it's supported in MONO, since the code should be portable to Linux as well).

2
  • WPF does use XML (XAML) to define GUIs, but it is not and will not be supported by Mono anytime soon (mono-project.com/WPF). Commented Apr 27, 2010 at 10:29
  • But a subset of XAML is supported by Mono via Moonlight. Commented Apr 27, 2010 at 10:35

5 Answers 5

4

Glade is a RAD tool to enable quick & easy development of user interfaces for the GTK+ toolkit and the GNOME desktop environment.

The user interfaces designed in Glade are saved as XML, and by using the GtkBuilder GTK+ object these can be loaded by applications dynamically as needed.

By using GtkBuilder, Glade XML files can be used in numerous programming languages including C, C++, C#, Vala, Java, Perl, Python,and others.

I've used glade with C# and I was pleased with the result. Glade probably won't suit you directly, but you can at least borrow some ideas from it.

Sign up to request clarification or add additional context in comments.

Comments

1

If you're going to use XML, then you should really know about XML schemas - these are XML files that describe the content of an XML file and DevStudio (and other editors) can read them and do autocompletion, which is useful. Also, you can validate an XML against a schema to ensure the content contains no structural errors.

Also, as Paul wrote, XAML is an XML system, but you'd need to use WPF framework to parse it.

Comments

0

WPF uses xml to define most things, it's known as xaml.

Comments

0

"Is there a tool, or a built-in code in the .NET environment which can do that for me automatically?"

There are various wrappers for .NET around XULRunner on code.google.com

Comments

0

Check out:

https://social.msdn.microsoft.com/Forums/en-US/554eefae-429f-495c-aee0-b2e971494ed0/how-do-i-create-a-gui-which-reads-xml-file-and-adds-controls-to-it-at-runtime?forum=csharplanguage

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WindowsFormsApplication_DynamicGUIFromXML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        XDocument guiConfig = XDocument.Load(@"../../Gui.xml");

        foreach (XElement item in from y in guiConfig.Descendants("Item") select y)
        {
            Control tmp = new Control();
            switch (item.Attribute("type").Value)
            {
                case "Button":
                    tmp = new Button();
                    break;
                case "TextBox":
                    tmp = new TextBox();
                    break;
            }

            tmp.Name = item.Attribute("name").Value;
            tmp.Text = item.Attribute("text").Value;
            tmp.Location = new Point(Int32.Parse(item.Attribute("x").Value), Int32.Parse(item.Attribute("y").Value));
            Controls.Add(tmp);
        }

    }
    }
 }

// ***********************************************
// Contents of Gui.xml
// ***********************************************
//<?xml version="1.0" encoding="utf-8" ?>
//<Gui>
//  <Item type="Button" name="foo" text="bar" x="100" y="100"  />
//  <Item type="TextBox" name="foo2" text="bar2" x="200" y="200"  />
//</Gui>
// ***********************************************

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.