将结构复杂的XML导入Dataset并遍历

最近在做省直单位“多证合一、一照一码”数据共享平台的本地接口开发工作,由于该“平台”其实就是个WebService故查询返回和主动推送的都是XML格式数据,且结构较为繁复,比如查询企业工商登记数据返回的XML格式如下:

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <!-- 一表申报数据,qytype:用于区分内外资企业(如“内资、外资”),
  3 project:企业申报项目(如”设立、变更、注销、备案”等) 
  4 appid:对应的应用ID由审批信息共享平台分配。-->
  5 <nts_data  qytype="" project="" appid="">
  6   <!--基本信息-->
  7   <basicinfo>
  8     <!-- 统一社会信用代码 18位-->
  9 <tyshxydm></tyshxydm>
 10 <!—工商注册号-->
 11 <gszch></gszch>
 12     <!-- 企业名称(中文)-->
 13     <cnname></cnname>
 14     <!-- 企业国别(外商投资企业填写)-->
 15     <companycountry></companycountry>
 16     <!-- 公司类型/企业类型-->
 17     <companytype></companytype>
 18     <!-- 单位性质-->
 19     <companyproperty></companyproperty>
 20     <!--经济类型-->
 21     <economictype></economictype>
 22     <!-- 注册(机构/住所/生产经营地址)地址-->
 23     <regaddress></regaddress>
 24     <!--生产经营地址 新增-->
 25     <manageaddress></manageaddress>
 26     <!--生产经营地所在行政区划 新增-->
 27     <manageareacode></manageareacode>
 28     <!-- 法人代表信息姓名  -->
 29     <legalpersonname></legalpersonname>
 30     <!-- 经营期限开始时间-->
 31     <managebegindate></managebegindate>
 32     <!-- 经营期限结束时间-->
 33     <manageenddate></manageenddate>
 34 <!-- 企业注册资本 amount:资本总额(填写具体数字) acurrency:币种  unit:币种单位,如万元-->
 35     <companyregfund amount="" acurrency="" unit=""/>
 36     <!-- 投资 amount:投资总额(填写具体数字) acurrency:币种 dollars:折万美元-->
 37     <companyinvest amount="" acurrency="" dollars=""/>
 38     <!-- 经营范围-->
 39     <managerange></managerange>
 40     <!-- 单位联系电话-->
 41     <companyphone></companyphone>
 42     <!-- 邮政编码-->
 43     <zipcode></zipcode>
 44     <!-- 单位邮箱-->
 45     <email></email>
 46     <!-- 网址-->
 47     <website></website>
 48     <!-- 职工人数/从业人数 (填写具体数字)-->
 49     <workernum></workernum>
 50     <!--busilicensenum:营业执照副本数量 (填写具体数字)-->
 51     <busilicensenum></busilicensenum>
 52     <!--isapplyelebusilicense:是否申请电子营业执照 (填1或者2 1代表是 2代表否)-->
 53     <isapplyelebusilicense></isapplyelebusilicense>
 54     <!--设立方式(股份公司填写)-->
 55     <establishway></establishway>
 56     <!--隶属企业名称(分支机构填写)-->
 57     <headcomp_name></headcomp_name>
 58     <!--隶属企业注册号(分支机构填写)-->
 59     <headcomp_regcode></headcomp_regcode>
 60     <!--合伙人数-->
 61     <partnernum></partnernum>
 62     <!--有限合伙人数-->
 63     <limitpartnernum></limitpartnernum>
 64     <!-- 成立日期-->
 65     <establishdate></establishdate>
 66     <!-- 核准日期-->
 67     <approvedate></approvedate>
 68     <!--行业名称-->
 69     <indurstryname></indurstryname>
 70     <!--行业编码-->
 71     <indurstrycode></indurstrycode>
 72     <!-- 登记状态(存续、)-->
 73     <registerstate></registerstate>
 74     <!-- 登记机关-->
 75     <proveins></proveins>
 76     <!-- 登记机关代码-->
 77     <proveinscode></proveinscode>
 78   </basicinfo>
 79   <!--投资者(股东、发起人等)-->
 80   <stockholders>
 81      <!--股东信息 name:股东姓名 type:股东类型 identificationtype:证件类型 identificationcode:证件号码 holdercountry:投资方国别-->
 82     <holder name="" type="" holdercountry=""  identificationtype="" identificationcode="">
 83       <!--出资(单位默认都是万元) way:出资方式 crncy:币种 amount_sub:认缴出资总额  ratio:认缴出资比例   amount_paid:实缴出资总额 foundarrivedate:资金到位期限 assessmentmethod:评估方式  responsibility:承担责任方式-->
 84       <invest way="" crncy="" amount_sub="" ratio="" amount_paid="" foundarrivedate="" assessmentmethod="" responsibility=""/>
 85     </holder>
 86   </stockholders>
 87   <!--董事 监事 经理-->
 88   <directorinfos>
 89     <!--人员信息 name:姓名 office:职务(执行董事、监事、经理)islegalperson:是否法定代表 country:国籍 *identificationtype:证件类型 identificationcode:证件号码
 90     createmode:产生方式  moblilephone:移动电话-->
 91     <director name="" office="" country="" islegalperson="" identificationtype="" identificationcode=""  createmode=""  moblilephone=""/>
 92   </directorinfos>
 93   <!--经办人信息 name:姓名 
 94   identificationtype:证件类型 identificationcode:证件号码 mobilephone:移动电话
 95   power:委托权限4位 每一位(0/1)代表不同意/同意-->
 96   <handingperson name=""  identificationtype="" identificationcode="" mobilephone=""  power=""/>
 97   <!--财务负责人  name:姓名  identificationtype:证件类型 
 98   identificationcode:证件号码 fixedphone:固定电话 新增 mobilephone:移动电话 email电子邮箱 新增 -->
 99   <financialperson name=""  identificationtype="" identificationcode=""  mobilephone=""  fixedphone=""  email=""/>
100   <!--办税人 name:姓名  identificationtype:证件类型 
101   identificationcode:证件号码 fixedphone:固定电话 mobilephone:移动电话-->
102   <taxperson name=""  identificationtype="" identificationcode=""  mobilephone=""/>
103   <!-- 指定或者委托有效期限  begindate:开始时间  enddate:结束时间-->
104   <entrustperiod begindate="" enddate=""/>
105   <!--税务登记信息-->
106   <taxreginfo>
107     <!--核算方式(填写1或者2 1代表独立核算 2代表非独立核算)-->
108     <accountway></accountway>
109     <!--适用会计制度(填写1或者2 1企业会计准则 2小企业会计准则)-->
110     <sykjzd></sykjzd>
111   </taxreginfo>
112 </nts_data>

在开发前期接口测试阶段不准备对数据做深入处理,只想通过简单的程序检测数据结构与省数据中心提供的那份让人抓狂的文档是否有出入,故写了以下程序对查询和推送内容简单解析、显示以供比对(为省事将各类XML数据保存成.xml文件以便测试):

 1 XmlDocument doc = new XmlDocument();
 2             //doc.Load(Server.MapPath("test.xml"));
 3             DataSet ds = new DataSet();
 4             ds.ReadXml(Server.MapPath("test.xml"));
 5             if (ds.Tables[0].Rows.Count > 0)
 6             {
 7                 foreach (DataTable dt in ds.Tables)
 8                 {
 9                     Msg.Text += "<br/>========== " + dt.TableName + " ==========";
10                     int n = 0;
11                     foreach (DataRow dr in dt.Rows)
12                     {
13                         if(n>0)
14                             Msg.Text += "<br/>----------------------------------------------";
15                         for (int i = 0; i < dt.Columns.Count; i++)
16                         {
17                             Msg.Text += ("<br/>" + dt.Columns[i].ColumnName + "" + dr[i].ToString());
18                         }
19                         n++;
20                     }
21                 }
22             }
23             ds.Clear();

以上为主要代码段,与常见的简单结构XML不同,测试的XML在导入DataSet后数据会被存放于若干个DataTable中,通过以上代码实现了对表、行、列的遍历和显示,并做了简单分割以使结构明晰。后续对数据的处理也可直接利用该方法实现。

部分显示结果如下:

posted @ 2017-10-22 20:59  iNetBug  阅读(289)  评论(0编辑  收藏  举报