2009-09-22 106 views
1

下面是我想读和创建基于这些结构在运行时的几个图形用户界面的样本XML,(其基本思想是从XML文件中读取的字段,并为屏幕用户输入)。读取XML文件创建一个UI

我写这外币在运行时的屏幕一些示例代码,但是我不知道怎么Ø做到这一点时,我想读从XML文件中的字段。

任何帮助真的很感激。

下面是示例C#代码来做到这一点(我想这样做的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; 
} 

下面是示例XML(我希望阅读和创建GUI看起来像数据输入屏幕)。

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

回答

3

第一件事第一件事。既然你想创建WPF,你为什么不使用XSLT将你的xml转换成XAML。

但是,如果这比你需要更多然后看看Linq2Xml