New一个clscolA/B(就叫colA/B吧)然后遍历文件或取一个包含所有记录(目标节点)的NodeList,然后作如下状:
With NodeList for i as integer=0 to .Count-1 Dim objson as clsA/B=colA/B.GenerateAson objson.Name1=.item(i).childnodes(x1).innertext objson.Name2=.item(i).childnodes(x2).innertext ...... ...... ...... colA/B.Addson(objson) next i End With |
Public Shared Function FillFromXml(ByVal NodeName As String, ByVal strPath As String, ByVal objFather As Object) As Boolean Try With xmlGetList(NodeName, strPath) For i As Integer = 0 To .Count - 1 Dim st As Object = objFather.GenerateAson Dim ty As Type = st.GetType For Each pp As PropertyInfo In ty.GetProperties With DirectCast(.Item(i), XmlElement) If .SelectSingleNode(pp.Name.ToUpper) IsNot Nothing Then pp.SetValue(st, CType(.SelectSingleNode(pp.Name.ToUpper).InnerText.Trim, String), Nothing) End If End With Next objFather.AddSon(st) Next End With Return True Catch ex As Exception Return False End Try End Function |
使用这个函数,只需要保证集合类clscolA/B有GenerateAson和Addson两个方法以及clsA/B两子类的属性名称与文件中Field保持一致即可。你只需要传递相应A或者B文件的目标节点名,文件路径,以及对应的集合类即可,此时再看一下赋值过程的代码减少了多少,恩,只有8行,这对于笔者则意味着省去了45-8=37行代码,而且丢弃老套的排比句。
第一阶段战斗算是小试牛刀了一把,如果只算赋值过程代码减少百分数为37/45,接近83%。
欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛
https://bbs.txwb.com
关注天下网吧微信/下载天下网吧APP/天下网吧小程序,一起来超精彩
|