2010-02-07 3 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(); 
      } 
     } 
    } 
} 

는 ... 위의 코드는 제로 오류를 생성합니다. 하지만, 그냥 작동하지 않습니다. 양식에 컨트롤이 표시되지 않습니다. 아무도 왜 위의 코드가 작동하지 않는지 알 수 있습니까? 어떻게 작동시킬 수 있습니까?

모든 도움을 감사합니다, 당신에게

Bael

감사 나는 당신에게 좀 더 일반적인 방법 제안
+0

코드를 통해 디버깅하면 어떻게됩니까? –

답변

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

고마워요. 나는 똑같은 문제가있다. 그것은 양식에 어떤 컨트롤을 표시하지 않습니다 ... –

+0

감사합니다 :) 그것은 지금 일하고 있습니다. –