1

所以我有一个将自定义用户控件加载到流面板中的按钮。我的问题是我试图让“保存”按钮通过并从所有用户控件中获取信息并将它们保存到 XML 文件中。我什至不知道从哪里开始。我已经从表单的已加载部分保存到 XML,但从未从动态加载的对象中保存。因此,每个用户控件中都有一组文本框,因此假设其中有 2 个已加载,XML 文件将如下所示:

<Operation>
  <OperationInfo>
    <OperationName>Name</OperationName>
    <OperationDate>mm/dd/yyy</OperationDate>
    <FC>Someone</FC>
  </OperationInfo>
  <Pilots>
     <Pilot>
      <PilotName>Name2</PilotName>
      <Ship>Ship</Ship>
      <StartTime>00:00</StartTime>
      <EndTime>00:00</EndTime>
      <Hours>0</Hours>
      <Pay>0</Pay>
    </Pilot>
    <Pilot>
      <PilotName>Name3</PilotName>
      <Ship>Ship</Ship>
      <StartTime>00:00</StartTime>
      <EndTime>00:00</EndTime>
      <Hours>0</Hours>
      <Pay>0</Pay>
    </Pilot>
  </Pilots>
</Operation>

如果我有第三个,就会有另一个“飞行员”实例

更新:在得到 Tariqulazam 的回答后,我得到了以下代码:

        public interface IData
    {
        string Pilotname { get; }
        string Ship { get; }
        DateTime StartTime { get; }
        DateTime EndTime { get; }
        decimal payperhour { get; }
    }
        protected void Page_Load(object sender, EventArgs e)
         {
              for (int i = 0; i < 5; i++)
                 {
                     Control ctrl = Form1.LoadControl("~/Dynamic.ascx");
                     order.Controls.Add(ctrl);
                 }
 }

        private static Control LoadControl(string p)
        {
            throw new NotImplementedException();
        }
 protected void OpSave_Click(object sender, EventArgs e)
 {
         foreach (var ctrl in order.Controls)
             {
                if (ctrl is IData)
                    {
                     //Grab data from the UserControl
                     string name = ((ctrl as IData).Pilotname);
                     string ship = ((ctrl as IData).Ship);
                     DateTime starttime = ((ctrl as IData).StartTime);
                     DateTime endtime = ((ctrl as IData).EndTime);
                     decimal pph = ((ctrl as IData).payperhour);
                     //Construct the XML file from the data.
                     using (StreamWriter sw = File.AppendText(string.Format(@"{0}\ops\op-" + OpName.Text.Replace(" ", "_") + ".xml", Directory.GetCurrentDirectory())))
                     {

                         DateTime dtStart = starttime;
                         DateTime dtEnd = endtime;

                         if (dtEnd < dtStart)
                         {
                             dtEnd = dtEnd.AddDays(1);
                         }

                         double hours = (dtEnd - dtStart).TotalHours;
                         double pay = hours * Convert.ToDouble(pph);
                         XElement root = new XElement("Operation",
                             new XElement("OperationName", OpName.Text),
                             new XElement("OperationDate", OpDate.Value.ToShortDateString()),
                             new XElement("Pilot",
                             new XElement("PilotName", name),
                             new XElement("ShipType", ship),
                             new XElement("StartTime", starttime),
                             new XElement("EndTime", endtime),
                             new XElement("HoursMined", hours),
                             new XElement("Pay", pay)));

                         sw.Write("{0}{1}", root, Environment.NewLine);
                     }
                     }
            }

 }

但是这不会保存,而且我认为它不能访问组框中的控件,有没有办法解决这个问题?因为所有需要访问的东西都在一个分组框中。(好吧,动态加载的每个都在自己的 groupbox 中,并且 OpName、FC 和 OpDate 都在同一个 groupbox 中)

4

1 回答 1

0

更新:我已经使用 Windows 窗体应用程序对此进行了测试。它正在正确创建 XML 文件。请在保存到 XML 文件之前验证输入。

那就是我会做的。创建这样的接口

public interface IData
{
    string PilotName { get; }
    string Ship { get; }
    DateTime StartTime { get; }
    DateTime EndTime { get; }
    Decimal Rate { get; }
}

在用户控件中实现接口,如下所示

public partial class Dynamic : UserControl, IData
{
    public Dynamic()
    {
        InitializeComponent();
    }


    public string PilotName
    {
        get { return txtName.Text; }
    }

    public string Ship
    {
        get { return txtShip.Text; }
    }

    public DateTime StartTime
    {
        get { return Convert.ToDateTime(txtStart.Text); }
    }

    public DateTime EndTime
    {
        get { return Convert.ToDateTime(txtEnd.Text); }
    }

    public decimal Rate
    {
        get { return Convert.ToDecimal(txtPayRate.Text); }
    }
}

用于从用户控件加载和检索值

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        LoadControls();
    }

    private void LoadControls()
    {
        for (int i = 0; i < 3; i++)
        {
            flPanel.Controls.Add(new Dynamic());
        }
    }


    private void btnSave_Click(object sender, EventArgs e)
    {
        XmlTextWriter writer = new XmlTextWriter(@"C:\Temp\Operation.xml", System.Text.Encoding.UTF8);
        writer.WriteStartDocument(true);
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 2;
        writer.WriteStartElement("Operation");
        writer.WriteStartElement("OperationInfo");
        writer.WriteStartElement("OperationName");
        writer.WriteString("Sample operation name");
        writer.WriteEndElement();
        writer.WriteStartElement("OperationDate");
        writer.WriteString(DateTime.Now.ToString("dd-MM-yyyy"));
        writer.WriteEndElement();
        writer.WriteStartElement("FC");
        writer.WriteString("Someone");
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteStartElement("Pilots");
        foreach (var ctrl in flPanel.Controls)
        {
            if (ctrl is IData)
            {
                writer.WriteStartElement("Pilot");
                var data = ctrl as IData;

                writer.WriteStartElement("PilotName");
                writer.WriteString(data.PilotName);
                writer.WriteEndElement();

                writer.WriteStartElement("Ship");
                writer.WriteString(data.Ship);
                writer.WriteEndElement();

                writer.WriteStartElement("StartTime");
                writer.WriteString(data.StartTime.ToString("hh:mm"));
                writer.WriteEndElement();

                writer.WriteStartElement("EndTime");
                writer.WriteString(data.EndTime.ToString("hh:mm"));
                writer.WriteEndElement();

                writer.WriteStartElement("Pay");
                writer.WriteString(data.Rate.ToString());
                writer.WriteEndElement();


                writer.WriteEndElement();
            }
        }
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Close();
        MessageBox.Show("XML File created!");
    }


}
于 2012-10-05T00:05:04.163 回答