2010-02-07 112 views
4

我有一個Xml文件。它的格式如下:閱讀和處理XML文件

ControlType > Content > LocationX > LocationY > ForeColor/LinkColor > Int > Int > Int > Int 

文件例如:

<?xml version="1.0" encoding="utf-8"?> 
<cs> 
    <Label Content="Double-click to edit." LocationX="583" LocationY="254" A="255" R="255" G="255" B="255" /> 
    <LinkLabel Content="Double-click to edit." LocationX="613" LocationY="251" A="255" R="0" G="0" B="0" /> 
</cs> 

背景:生成一個XML文件,並保存在磁盤上。當用戶將Xml文檔加載到我的應用程序中時,我的應用程序將讀取Xml文件。而在文檔中的foreach控制,將檢索它的屬性,就像這樣:

foreach(Control control in XmlFile) 
{ 
    // get control type 
    // get control content 
    // get LocationX 
    // get LocationY 
    // get Color 
    // get Int 
    // get Int 
    // get Int 
    // get Int 

    // Do something with retrieved data. 
} 

這裏是我已經有:

OpenFileDialog o = new OpenFileDialog(); 

o.Filter = 
    "T Multimedia Format (*.mf)|*.mf|" + 
    "Word Document (*.docx)|*.docx|" + 
    "PDF Document (*.pdf)|*.pdf|" + 
    "Text FIle (*.txt)|*.txt"; 
o.Title = "T 11 - Open Document"; 

using (o) 
{ 
    if (o.ShowDialog() == DialogResult.OK) 
    { 
     XDocument xdc = XDocument.Load(o.FileName); 

     var cs = xdc.Elements("cs"); 
     foreach (var im in cs) 
     { 
      if (im.Name == "Label") 
      { 
       Label label = new Label(); 
       label.MouseClick += new MouseEventHandler(label_MouseClick); 
       label.MouseDown += new MouseEventHandler(label_MouseDown); 
       label.MouseMove += new MouseEventHandler(label_MouseMove); 
       label.MouseUp += new MouseEventHandler(label_MouseUp); 
       label.MouseDoubleClick += new MouseEventHandler(label_MouseDoubleClick); 

       label.Text = im.Attribute("Content").Value; 

       label.Location = new Point(
        Convert.ToInt32(im.Attribute("LocationX").Value), 
        Convert.ToInt32(im.Attribute("LocationY").Value)); 

       label.BackColor = Color.Transparent; 
       label.ForeColor = Color.FromArgb(
        Convert.ToInt32(im.Attribute("A").Value), 
        Convert.ToInt32(im.Attribute("R").Value), 
        Convert.ToInt32(im.Attribute("G").Value), 
        Convert.ToInt32(im.Attribute("B").Value)); 

       label.AutoSize = true; 
       Canvas.Controls.Add(label); 

       label.BringToFront(); 
      } 
      else if (im.Name == "LinkLabel") 
      { 
       LinkLabel link = new LinkLabel(); 
       link.MouseClick += new MouseEventHandler(link_MouseClick); 
       link.MouseDown += new MouseEventHandler(link_MouseDown); 
       link.MouseMove += new MouseEventHandler(link_MouseMove); 
       link.MouseUp += new MouseEventHandler(link_MouseUp); 
       link.MouseDoubleClick += new MouseEventHandler(link_MouseDoubleClick); 

       link.Text = im.Attribute("Content").Value; 

       link.Location = new Point(
        Convert.ToInt32(im.Attribute("LocationX").Value), 
        Convert.ToInt32(im.Attribute("LocationY").Value)); 

       link.BackColor = Color.Transparent; 
       link.LinkColor = Color.FromArgb(
        Convert.ToInt32(im.Attribute("A").Value), 
        Convert.ToInt32(im.Attribute("R").Value), 
        Convert.ToInt32(im.Attribute("G").Value), 
        Convert.ToInt32(im.Attribute("B").Value)); 

       link.AutoSize = true; 
       Canvas.Controls.Add(link); 

       link.BringToFront(); 
      } 
     } 
    } 
} 

...上面的代碼生成零個誤差。但是,它不起作用。表單上沒有顯示任何控件。有人知道爲什麼上面的代碼不起作用,我怎麼才能使它工作?

所有幫助表示讚賞,謝謝

巴爾

+0

當您通過代碼進行調試時會發生什麼? – 2010-02-07 20:42:57

回答

3

我建議你一點更通用的方法:

private void Form1_Load(object sender, EventArgs e) 
{ 
    foreach (var controlTag in XDocument.Load("settings.xml").Root.Elements()) 
    { 
     var controlType = Type.GetType(string.Format("System.Windows.Forms.{0}, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", controlTag.Name.LocalName), false); 
     if (controlType == null || !typeof(Control).IsAssignableFrom(controlType)) 
     { 
      continue; 
     } 
     var control = (Control)Activator.CreateInstance(controlType); 
     control.Text = controlTag.Attribute("Content").Value; 
     control.Location = new Point(
      int.Parse(controlTag.Attribute("LocationX").Value), 
      int.Parse(controlTag.Attribute("LocationY").Value) 
     ); 
     control.BackColor = Color.Transparent; 

     control.MouseClick += mouseClick; 
     control.MouseDown += mouseDown; 
     control.MouseMove += mouseMove; 
     control.MouseUp += mouseUp; 
     control.MouseDoubleClick += mouseDoubleClick; 

     Controls.Add(control); 
    } 

} 

至於顏色,你可以將屬性添加到XML文件這將指向我們想要設置和使用反射的控件的屬性名稱。例如:

<?xml version="1.0" encoding="utf-8"?> 
<cs> 
    <Label Content="Double-click to edit." LocationX="583" LocationY="254" A="255" R="255" G="255" B="255" ColorProperty="ForeColor" /> 
    <LinkLabel Content="Double-click to edit." LocationX="613" LocationY="251" A="255" R="0" G="0" B="0" ColorProperty="LinkColor" /> 
</cs> 

順便說一句,XAML已經提供了許多您在此嘗試實現的功能。當然,它假定WPF接口可能不是你的情況。

+0

謝謝你。我有完全一樣的問題。它不顯示窗體上的任何控件... – 2010-02-07 20:41:47

+0

謝謝:)它現在工作。 – 2010-02-07 20:48:30