#region 读写xml配置文件
public void ReadPrintConfig()
{
string sFileName = Application.StartupPath + @"\" + this.Text + ".xml";
if (!File.Exists(sFileName)) return;
//读取打印机参数
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(sFileName);
//
XmlNode PrinterParam = xmldoc.SelectSingleNode("PrintConfig/PrinterParam");
if (PrinterParam != null)
{
XmlNode xesub1 = PrinterParam.SelectSingleNode("PrinterName");
if (xesub1 != null) this.cbxSelectPrinter.Text = xesub1.InnerText;
XmlNode xesub2 = PrinterParam.SelectSingleNode("PaperSize");
if (xesub2 != null)
{
XmlElement xe = (XmlElement)xesub2;
this.cbxPaperKind.Text = xe.GetAttribute("Kind");
this.nudPaperWidth.Value = Math.Max(Convert.ToInt32(xe.GetAttribute("Width")), 50);
this.nudPaperHeight.Value = Math.Max(Convert.ToInt32(xe.GetAttribute("Height")), 50);
}
}
//读取打印项目位置
XmlNode PrintItemList = xmldoc.SelectSingleNode("PrintConfig/PrintItemList");
if (PrintItemList == null) return;
XmlNodeList xnl = PrintItemList.ChildNodes;
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
string sName = xe.Name;
int x = Convert.ToInt32(xe.GetAttribute("X"));
int y = Convert.ToInt32(xe.GetAttribute("Y"));
//
//MessageBox.Show(sName);
Control[] ctrlList = this.Controls.Find(sName, true);
if (ctrlList.Length > 0)
{
ctrlList[0].Left = x;
ctrlList[0].Top = y;
}
}
}
private void WritePrintConfig()
{
XmlDocument xmldoc = new XmlDocument();
//xml声明行
var xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmldoc.AppendChild(xmlnode);
//打印配置
XmlNode PrintConfig = xmldoc.CreateNode(XmlNodeType.Element, "PrintConfig", null);
xmldoc.AppendChild(PrintConfig);
//打印机参数
XmlNode PrinterParam = xmldoc.CreateNode(XmlNodeType.Element, "PrinterParam", null);
PrintConfig.AppendChild(PrinterParam);
XmlElement xesub1 = xmldoc.CreateElement("PrinterName");
xesub1.InnerText = this.cbxSelectPrinter.Text;
PrinterParam.AppendChild(xesub1);
XmlElement xesub2 = xmldoc.CreateElement("PaperSize");
xesub2.SetAttribute("Kind", this.cbxPaperKind.Text);
xesub2.SetAttribute("Width", this.nudPaperWidth.Value.ToString());
xesub2.SetAttribute("Height", this.nudPaperHeight.Value.ToString());
PrinterParam.AppendChild(xesub2);
//打印项目列表
XmlNode PrintItemList = xmldoc.CreateNode(XmlNodeType.Element, "PrintItemList", null);
PrintConfig.AppendChild(PrintItemList);
foreach (Control ctrl in this.Controls)
{
//MessageBox.Show(ctrl.Name);
if (ctrl is Label)
{
XmlElement xe = xmldoc.CreateElement(ctrl.Name);
xe.SetAttribute("X", ctrl.Left.ToString());
xe.SetAttribute("Y", ctrl.Top.ToString());
PrintItemList.AppendChild(xe);
}
}
//保存
string sFileName = Application.StartupPath + @"\" + this.Text + ".xml";
if (File.Exists(sFileName)) File.Delete(sFileName);
xmldoc.Save(sFileName);
MessageBox.Show("打印配置已成功保存到文件:\n\n" + sFileName);
}
#endregion
生成的XML文件格式如下:
<?xml version="1.0"?>
<PrintConfig>
<PrinterParam>
<PrinterName>Microsoft XPS Document Writer</PrinterName>
<PaperSize Kind="自定义" Width="241" Height="152" />
</PrinterParam>
<PrintItemList>
<lbl备注 X="538" Y="441" />
<lbl税务机关 X="554" Y="81" />
<lbl纳税人识别号 X="151" Y="117" />
<lbl填票人 X="156" Y="468" />
<lbl金额合计 X="732" Y="333" />
<lbl金额合计大写 X="151" Y="387" />
<lbl税种 X="151" Y="230" />
<lbl实缴金额 X="731" Y="230" />
<lbl品目名称 X="330" Y="231" />
<lbl地址 X="151" Y="165" />
<lbl填开日期 X="323" Y="81" />
<lbl纳税人名称 X="395" Y="114" />
</PrintItemList>
</PrintConfig>
确实很方便,比使用INI文件灵活多了!