tush1r
tush1r

Reputation: 19593

Reading an XML File create a UI

Below is a sample XML which I want to read and create a few GUIs based on these structures at runtime, (the basic idea is to read the fields from a XML file and create the screen for the user input).

I have written some sample code which creats the screen at run time, however i am not sure how o do this when i wish to read the fields from the XML file.

Any help is really appreciated.

Below is the sample C# code to do this (I want to do this in WPF).

private void Init()
{
    StackPanel spParent = new StackPanel();
    StackPanel sp;
    for (int i = 0; i < 5; i++)
    {
        sp = CreateLabelTextPair(i);
        spParent.Children.Add(sp);
    }
    spParent.Orientation = Orientation.Vertical;
    spParent.Margin = new Thickness(2);

    this.Content = spParent;
    this.Height = spParent.Height + 10;
    this.Width = spParent.Width + 10;
}

StackPanel CreateLabelTextPair(int i)
{
    StackPanel sp = new StackPanel();

    Label lbl = new Label();
    lbl.Height = 25;
    lbl.Width = 100;
    lbl.Content = "Label" + (i+1);
    sp.Children.Add(lbl);

    sp.Margin = new Thickness(2);

    TextBox tb = new TextBox();
    tb.Height = 25;
    tb.Width = 100;
    tb.Text = "TextBox" + (i+1);
    sp.Children.Add(tb);

    sp.Height = lbl.Height;
    sp.Width = lbl.Width+tb.Width+10;

    sp.Orientation = Orientation.Horizontal;
    return sp;
}

Below is the sample XML (which i wish to read and create the GUI look like a data entry screen).

  <DataSet>
      <Data>
        <Field1>Name</Field1>
        <Field2>DataType</Field2>
        <Field3>Length</Field3>
        <Field4>DefaultValue</Field4>
        <Field5>IsNull</Field5>
        <Field6>Precesion</Field6>
      </Data>
  </DataSet>

Upvotes: 1

Views: 1041

Answers (1)

Preet Sangha
Preet Sangha

Reputation: 65516

First things first. Since you want create WPF, why don't you use XSLT to transform your xml into XAML.

However if this more than you need then have a look at Linq2Xml.

Upvotes: 3

Related Questions