假设我们现在配置了一个这样的XML文件,文件名为ReportConfig.xml
View Code
1 <?xml version="1.0" encoding="utf-8" ?> 2 <Reports> 3 <Report name="JZ_FSJZW" title="建设征地集镇房屋附属建筑物调查表" rowPerPage="34"> 4 <Head> 5 <Field name="A1" warpText="调查表标题" valueIndex ="0"/> 6 <Field name="C2" warpText="设计阶段" valueIndex ="1"/> 7 </Head> 8 <Data startWriteDataRowIndex="7"> 9 <Field name="A" colIndex="1" valueIndex="1" warpText="序号" /> 10 <Field name="B" colIndex="2" valueIndex="2" warpText="项目名称" /> 11 </Data> 12 <Sign rowHeight="20" pzxzfl="NCFW_FSJZW_LXGM"> 13 <Field name="QSR" startColName="A" endColName="B" fontSize="9" align="right" rowIndex="1"/> 14 </Sign> 15 <Sql> 16 <![CDATA[select * from t_cj_hz_fsjzw where HZXX_ID='@HZXX_ID']]> 17 </Sql> 18 </Report> 19 </Reports>
那么我们如何实现根据Report结点里的name属性值,得到相应的rowPerPage属性值呢!下面给大家提供几个方法:
View Code
1 /// <summary> 2 /// 根据Repotr结点的name属性,获得rowPerPage值 3 /// </summary> 4 /// <param name="reportName">属性name值</param> 5 /// <returns>rowPerPage值</returns> 6 public static int GetRowPerPage(string reportName) 7 { 8 int rowPerPage = 0; 9 XmlNode node = GetReportNode(reportName); 10 if (isContainsAttribute(node, "rowPerPage")) 11 { 12 int.TryParse(node.Attributes["rowPerPage"].Value, out rowPerPage); 13 } 14 return rowPerPage; 15 } 16 17 /// <summary> 18 /// 获取报表配置的相应XmlNode 19 /// </summary> 20 /// <param name="reportName">报表名</param> 21 /// <param name="nodeName">配置文件中的节点名,如需要获取Report节点,则不需要传入此参数</param> 22 /// <returns>返回报表配置的相应XmlNode</returns> 23 public static XmlNode GetReportNode(string reportName, string nodeName = "") 24 { 25 XmlDocument _objXmlDoc = new XmlDocument(); 26 //StartupPath应用程序的可执行程序路径,也就是bin\Debug下,注意一定要将ReportConfig.xml文件复制到‘输出目录’属性设置为‘始终复制’才能获取到该文件 27 _objXmlDoc.Load(System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "ReportConfig.xml")); 28 string xpath = "Reports//Report[@name='" + reportName + "']"; 29 if (!string.IsNullOrEmpty(nodeName)) 30 { 31 xpath += "//" + nodeName; 32 } 33 return _objXmlDoc.SelectSingleNode(xpath); 34 } 35 36 /// <summary> 37 /// 判断指定的xml节点中是否包含指定的属性 38 /// </summary> 39 /// <param name="xmlnode">xml节点</param> 40 /// <param name="attributeName">属性名</param> 41 /// <returns>判断指定的xml节点中是否包含指定的属性,如果包含则返回true,否则返回false</returns> 42 public static bool isContainsAttribute(System.Xml.XmlNode xmlnode, string attributeName) 43 { 44 bool flag = false; 45 for (int i = 0; i < xmlnode.Attributes.Count; i++) 46 { 47 if (xmlnode.Attributes[i].Name == attributeName) 48 { 49 flag = true; 50 break; 51 } 52 } 53 return flag; 54 }
下面我们就来实现一个简单的功能吧!
要实现的功能是,当我们改变 ReportName时,要得到对应的rowPerPage值。(我们可以多配几个Report结点来看效果)
View Code
1 private void Form1_Load(object sender, EventArgs e) 2 { 3 XmlDocument doc = new XmlDocument(); 4 doc.Load(System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, "ReportConfig.xml")); 5 //获取Reports下的所有Report结点 6 XmlNodeList nodes = doc.SelectNodes("Reports//Report"); 7 for (int i = 0; i < nodes.Count; i++) 8 { 9 for (int j = 0; j < nodes[i].Attributes.Count; j++) 10 { 11 if (nodes[i].Attributes[j].Name == "name") 12 { 13 cbx_ReportName.Items.Add(nodes[i].Attributes[j].Value); 14 } 15 } 16 } 17 18 //cbx_ReportName.SelectedValueChanged += (action, b) => 19 // { 20 // textBox1.Text = XMLFunction.GetRowPerPage(cbx_ReportName.SelectedItem.ToString()).ToString(); 21 // }; 22 23 //此行代码+cbx_ReportName_SelectedValueChanged事件等于上面注释的代码 24 cbx_ReportName .SelectedValueChanged +=new EventHandler(cbx_ReportName_SelectedValueChanged); 25 } 26 27 private void cbx_ReportName_SelectedValueChanged(object sender, EventArgs e) 28 { 29 textBox1.Text = XMLFunction.GetRowPerPage(cbx_ReportName.SelectedItem.ToString()).ToString(); 30 }