读写XML文件
发布时间:2020-12-16 07:39:53 所属栏目:百科 来源:网络整理
导读:#region 读写xml配置文件 public void ReadPrintConfig() { string sFileName = Application.StartupPath + @"" + this.Text + ".xml"; if (!File.Exists(sFileName)) return; //读取打印机参数 XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(sFile
#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("打印配置已成功保存到文件:nn" + 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文件灵活多了! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- flex socket.writeObject()发送对象,java接收flex socket发
- postgresql 语句的汇总
- Oracle创建用户、赋予权限的过程
- c# – 创建一个异步方法,包装订阅并在总线上发布
- 在Oracle sql中“%Type”是什么意思?
- 需要显示一个虚拟月份 – Oracle SQL
- failed parsing /data/system/wallpaper_info.xml java.io.
- jsonp其实很简单【ajax跨域请求】
- applciation.xml foundation configuration and descriptio
- c# – Exchange Web服务管理API:访问其他用户项目