2009-09-22 101 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