获取XML配置数据

时间:2020-12-08 22:52:51

XML结构:

<Setting>
  <BIG>
    <tdHead>
      <td TdName="序号" TdWidth="50px" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="公司" TdWidth="60px" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="线路" TdWidth="90px" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="牌照号" TdWidth="" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="自编号" TdWidth="" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="开始时间" TdWidth="120px" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="结束时间" TdWidth="120px" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="超速时长(秒)" TdWidth="80px" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="超速里程(公里)" TdWidth="80px" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="最高速度(km/h)" TdWidth="90px" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="平均车速(km/h)" TdWidth="90px" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="超速标准(km/h)" TdWidth="70px" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
      <td TdName="超速位置" TdWidth="" TdHeight="" TdTitle="" TdRowspan="" TdColspan=""></td>
    </tdHead>
  </BIG>

</Setting>

/// <summary>
        /// 获取到xml数据集中所需节点数据
        /// </summary>
        /// <param name="Path"></param>
        /// <param name="NodeName"></param>
        /// <returns></returns>
        public List<List<MonitorTableHead>> GetXMLData(string Path,string NodeName)
        {
            List<List<MonitorTableHead>> listLMont = new List<List<MonitorTableHead>>();
            try
            {
                //string str1 =Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe 的文件名。
                //string str2=Environment.CurrentDirectory;//获取和设置当前目录(即该进程从中启动的目
                ////录)的完全限定路径。
                ////备注  按照定义,如果该进程在本地或网络驱动器的根目录中启动,则此属性的值为驱
                ////动器名称后跟一个尾部反斜杠(如"C:\")。如果该进程在子目录中启动,则此属性的值为不
                ////带尾部反斜杠的驱动器和子目录路径(如"C:\mySubDirectory")。
                //string str3=Directory.GetCurrentDirectory();//获取应用程序的当前工作目录。
                //string str4=AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决
                //////程序用来探测程序集。
                ////string str5=System.Net.Mime.MediaTypeNames.Application.StartupPath;//获取启动了应用程序的可执行文件的路径,不包括可
                //////执行文件的名称。
                ////string str6=System.Net.Mime.MediaTypeNames.Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可
                //////执行文件的名称。
                //string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含
                ////该应用程序的目录的名称。
                XmlDocument doc = new XmlDocument();
                doc.Load(AppDomain.CurrentDomain.BaseDirectory + Path);
                XmlNodeList nodeList = ((XmlElement)doc.SelectSingleNode("Setting")).SelectSingleNode(NodeName).ChildNodes;
                //遍历所有子节点
                foreach (XmlNode xn in nodeList)
                {
                    List<MonitorTableHead> listMon = new List<MonitorTableHead>();
                    XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型
                    if (xe.Name == "tdHead")
                    {
                       
                        //xe.Attributes["id"].Value = "ssss";//修改节点属性的值。
                        //xe.SetAttribute("genre", "update Sky_Kwolf"); //则修改该属性为“update Sky_Kwolf”
                        XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点                      
                        foreach (XmlNode xn1 in nls)//遍历
                        {
                            MonitorTableHead mont = new MonitorTableHead();
                            XmlElement xe2 = (XmlElement)xn1; //转换类型
                            mont.TdName = xe2.Attributes["TdName"].Value;
                            mont.TdWidth = xe2.Attributes["TdWidth"].Value;
                            mont.TdHeight = xe2.Attributes["TdHeight"].Value;
                            mont.TdTitle = xe2.Attributes["TdTitle"].Value;
                            mont.TdRowspan = xe2.Attributes["TdRowspan"].Value;
                            mont.TdColspan = xe2.Attributes["TdColspan"].Value;
                            listMon.Add(mont);
                        }
                    }
                    listLMont.Add(listMon);
                }
            }
            catch (Exception)
            {
                return null;
                //throw;
            }
            return listLMont;
        }