   [XNA] UI design: How to interface between code and XML?
  I'm fairly new to XNA and C#, but not to programming in general (I have some years of C/C++ behind me).I'm setting up a UI system for my game which is laid out in XML files. A layout file declares some components and sets up their parameters. A simple example might look like this:<UI>    <Button>   <PosX>20</PosX>   <PosY>40</PosY>   <Label>My Button</Label>    </Button></UI>This is working peachily, and now I'm on to figuring out how to make that button (and other components) actually *do* something... which brings me to my question.I'm looking for some suggestions on how to implement this interface between code and XML. So maybe my layout file has a tag like <OnClick> that defines a handler function for when the component is clicked. But how is that handler defined?I could use a numeric ID, and then in code look up a handler function based on that ID. But that seems... clunky. I can see how to do it but it seems like there must be a better way.Is there something inherent to C# that would handily address this problem? I've been reading docs and such trying to find something, but like I said, I'm pretty new to C# so there's a lot of unfamiliar stuff to wade through.How have others handled this kind of thing?   
  I would probably hardcode the behavior (or script it using some other system), but XML should probably only be used for the layout of the GUI.  You could refer to the GUI elements in code through the labels, or simply an identifier.  (using the labels/names would probably be best)   ;  I agree: XML should be used only for the layout of the UI. If you really want to set up the names of the event handling methods, the only way is to use Reflection. Using reflection, you can get a reference to a method by its name, searching for it with the string value specified in the XML file. With that you can then run it. But it cause a performance hit.About the layout of the XML file, I've a suggestion. I believe that for "semantical clarity" you should only use child elements when the new element is semantically a child. In other words: have only container control specify children and treat all the fields as attributes. This kind of behaviour is also adopted by the ASP.NET pages for example which are basically UI definitions. This is a sample of the UI definition that I'm using for my own project (which is MDX and SlimDX only :D no xna!)    <Window Id="Window" Position="X:100 Y:100" Size="Width:640 Height:480" IsEnabled="true" IsVisible="true" TextStyleClass="WindowCaptionBar" ControlStyleClass="Window" Title="Test Window #1"> <Controls>   <Label Id="LabelTestPanel" Position="X:10 Y:10" IsEnabled="true" IsVisible="true" TextStyleClass="Default" Text="The blue window is a Panel Control and this is a Label" />   <TextBox Id="TextBox" Position="X:10 Y:150" Size="Width:200 Height:30" IsEnabled="true" IsVisible="true" TextStyleClass="TextBox" ControlStyleClass="TextBox" Text="This is a TextBox" AcceptsReturn="false" KeyMask="Letter Digit LetterOrDigit Punctuation Separator" />   <TrackBar Id="TrackBar" Position="X:20 Y:100" Size="Width:200 Height:30" IsEnabled="true" IsVisible="true" TextStyleClass="Default" ControlStyleClass="TrackBar" MinimumValue="0" MaximumValue="10" TickFrequency="2" />   [...] </Controls>    </Window>;  The reflection API in C# will allow you to find a function using its name. So you can store the name in the XML file and use the relfection API to find the function it refers to.   ;  An alternative to using reflection would be to code the name of a function and a reference to an external C# script file that would get loaded and compiled by the program at runtime. That way, you could write the functionality of the UI in C#, but still get the benefits of having it compiled on the fly.If you would like to use this method, check out the thread here on GameDev called Easy .NET Scripting.   