所以我有一个按钮,可以将自定义Usercontrol加载到流程面板中。我的问题是我正在尝试“保存”按钮,并从所有用户控件中获取信息并将其保存到XML文件中。我真的不知道从哪里开始。我已从已加载的表单部分保存到XML,但从未从动态加载的对象保存。 因此每个Usercontrols中都有一组文本框,所以说有两个加载的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>
如果我有第3名,那么会有另一个“Pilot”实例
更新:在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);
}
}
}
}
然而,这不会保存,我不认为它可以访问Group Box中的控件,有没有办法解决这个问题?因为所有需要访问的东西都在groupbox中。 (动态加载的每个都在自己的组框中,OpName,FC和OpDate都在同一个组框中)
答案 0 :(得分: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!");
}
}