<?xml version="1.0" encoding="GB2312" standalone="yes" ?> 
- <AreaCodeConf>
- <state Name="安徽省">
- <City Name="安庆" AreaCode="1309">
  <Branch Name="安徽省安庆岳西县城中分理处" DispatchNo="019" /> 
  <Branch Name="安徽省安庆枞阳县支行营业部" DispatchNo="018" /> 
  <Branch Name="安徽省安庆分行宿松县城中分理处" DispatchNo="016" /> 
  <Branch Name="安徽省安庆潜山县支行营业部" DispatchNo="013" /> 
  <Branch Name="安徽省安庆怀宁县支行营业部" DispatchNo="012" /> 
  <Branch Name="安徽省安庆望江县支行营业部" DispatchNo="011" /> 
  <Branch Name="安徽省桐城市支行龙眠分理处" DispatchNo="010" /> 
  <Branch Name="安庆分行汇通办事处" DispatchNo="023" /> 
  <Branch Name="安徽省安庆分行怀宁县支行月山分理处" DispatchNo="021" /> 
  <Branch Name="安徽省安庆分行怀宁县支行高河分理处" DispatchNo="020" /> 
  <Branch Name="安徽省安庆分行城建办事处" DispatchNo="501" /> 
  <Branch Name="安徽省安庆分行核算中心" DispatchNo="500" /> 
  <Branch Name="安徽省桐城市支行营业部" DispatchNo="009" /> 
  <Branch Name="安徽省安庆分行安铜办事处*" DispatchNo="008" /> 
  <Branch Name="安徽省安庆分行开发区办事处" DispatchNo="007" /> 
  <Branch Name="安徽省安庆分行石化集贤办事处" DispatchNo="006" /> 
  <Branch Name="安徽省安庆分行人民路办事处" DispatchNo="005" /> 
  <Branch Name="安徽省安庆分行西纺分理处*" DispatchNo="004" /> 
  <Branch Name="安徽省安庆分行华中路办事处" DispatchNo="003" /> 
  <Branch Name="安徽省安庆分行司下坡办事处" DispatchNo="002" /> 
  <Branch Name="安徽省安庆分行营业部" DispatchNo="001" /> 
  </City>
- <City Name="蚌埠" AreaCode="1303">
  <Branch Name="蚌埠分行直属分理处" DispatchNo="011" /> 
  <Branch Name="安徽省蚌埠分行固镇分理处" DispatchNo="009" /> 
  <Branch Name="蚌埠分行汇通办事处" DispatchNo="010" /> 
  <Branch Name="安徽省蚌埠五河县支行" DispatchNo="008" /> 
  <Branch Name="安徽省蚌埠怀远县支行" DispatchNo="007" /> 
  <Branch Name="安徽省蚌埠分行涂山办事处" DispatchNo="006" /> 
  <Branch Name="安徽省蚌埠分行南岗办事处" DispatchNo="005" /> 
  <Branch Name="安徽省蚌埠分行烟墩孜办事处" DispatchNo="004" /> 
  <Branch Name="安徽省蚌埠分行东风办事处" DispatchNo="003" /> 
  <Branch Name="安徽省蚌埠分行淮河办事处" DispatchNo="002" /> 
  <Branch Name="安徽省蚌埠分行营业部" DispatchNo="001" /> 
  <Branch Name="安徽省蚌埠分行核算中心" DispatchNo="500" /> 
  </City>。
 </state>
</AreaCodeConf>怎么去读取,我是要用AreaCode和DispatchNo作为条件读取City Name和Branch Name的值
请大侠赐教。

解决方案 »

  1.   

    安装MSXML4.0,然后引用该类库,就可以读取与写入数据了
      

  2.   

    比如实现类似select City Name,Branch Name from [xmldata] where AreaCode=数值 and DispatchNo=数值 的效果,能烦恼给几行代码。
      

  3.   

    嘿嘿....一个半小时前开始研究什么XML,弄了个函数,很不像样......:Private Function getXMLName(st1 As String, st2 As String) As String
    Dim XML As DOMDocument  'xml对象
    Dim XMLRoot As IXMLDOMElement '根节点
    Dim XMLNode As IXMLDOMElement '子节点
    Dim i As Integer, j As IntegerSet XML = New DOMDocument
    If XML.Load(App.Path & "/test.xml") = False Then
        MsgBox "打开XML文件出错!"
        Set XML = Nothing
        Exit Function
    End IfSet XMLRoot = XML.documentElement
    Set XMLNode = XMLRoot.selectSingleNode("/AreaCodeConf/state")
    If Not XMLNode Is Nothing Then
        For i = 0 To XMLNode.childNodes.length
            If XMLNode.childNodes(i).Attributes.Item(1).nodeValue = st1 Then
                For j = 0 To XMLNode.childNodes(i).childNodes.length
                    If XMLNode.childNodes(i).childNodes(j).Attributes.Item(1).nodeValue = st2 Then
                        getXMLName = XMLNode.childNodes(i).childNodes(j).Attributes.Item(0).nodeValue
                        Exit Function
                    End If
                Next j
            End If
        Next i
    End IfSet XML = Nothing
    End FunctionPrivate Sub Command1_Click()
    Text1.Text = getXMLName("1309", "500")
    End Sub
      

  4.   

    感觉XML就像个层次结构的数据库,不好用!没有关系型数据那么顺当.估计调用某一次的某个属性值会有便当的方法,我只是死K出来的,一个个调用下去,而且还是知道哪个属性是你要的.建议再请教XML高手!
      

  5.   

    对了,还差个city:
    Private Function getXMLName(st1 As String, st2 As String, st3 As String) As String
    Dim XML As DOMDocument  'xml对象
    Dim XMLRoot As IXMLDOMElement '根节点
    Dim XMLNode As IXMLDOMElement '子节点
    Dim i As Integer, j As IntegerSet XML = New DOMDocument
    If XML.Load(App.Path & "/test.xml") = False Then
        MsgBox "打开XML文件出错!"
        Set XML = Nothing
        Exit Function
    End IfSet XMLRoot = XML.documentElement
    Set XMLNode = XMLRoot.selectSingleNode("/AreaCodeConf/state")
    If Not XMLNode Is Nothing Then
        For i = 0 To XMLNode.childNodes.length
            If XMLNode.childNodes(i).Attributes.Item(1).nodeValue = st1 Then
                For j = 0 To XMLNode.childNodes(i).childNodes.length
                    If XMLNode.childNodes(i).childNodes(j).Attributes.Item(1).nodeValue = st2 Then
                        getXMLName = XMLNode.childNodes(i).childNodes(j).Attributes.Item(0).nodeValue
                        st3 = XMLNode.childNodes(i).Attributes.Item(0).nodeValue
                        Exit Function
                    End If
                Next j
            End If
        Next i
    End IfSet XML = Nothing
    End FunctionPrivate Sub Command1_Click()
    Dim st As String
    Text1.Text = getXMLName("1309", "500", st)
    Text2.Text = st
    End Sub
      

  6.   


    '需要引用Microsoft XML,version 2.0
    Private Sub Command1_Click()
        MsgBox GetXmlValue("1309", "500")
    End SubPrivate Function GetXmlValue(strAreaCode As String, strDispatchNo As String) As String
        On Error GoTo ToExit '打开错误陷阱
        '------------------------------------------------
        Dim xmlDocum As New DOMDocument
        Dim xmlNodeList As IXMLDOMNodeList
        Dim XMLNode As IXMLDOMNode
        Dim intI As Integer, intJ As Integer    GetXmlValue = ""
        xmlDocum.async = False
        If xmlDocum.Load(App.Path & "\ah.xml") = False Then
            MsgBox "XML载入失败!"
            Set xmlDocum = Nothing
            Exit Function
        End If    Set xmlNodeList = xmlDocum.getElementsByTagName("City")
        For intI = 0 To xmlNodeList.length - 1
            Set XMLNode = xmlNodeList.Item(intI)
            If XMLNode.Attributes(1).Text = strAreaCode Then
                For intJ = 0 To XMLNode.childNodes.length - 1
                    If XMLNode.childNodes(intJ).Attributes(1).Text = strDispatchNo Then
                        GetXmlValue = XMLNode.Attributes(0).Text & "   " & XMLNode.childNodes(intJ).Attributes(0).Text
                    End If
                Next
            End If
        Next    Set xmlNodeList = Nothing
        Set XMLNode = Nothing
        Set xmlDocum = Nothing
        '------------------------------------------------Exit Function    '----------------
    ToExit:
        Resume Next
    End Function
      

  7.   

    少写了一句,呵呵'需要引用Microsoft XML,version 2.0
    Private Sub Command1_Click()
        MsgBox GetXmlValue("1309", "500")
    End SubPrivate Function GetXmlValue(strAreaCode As String, strDispatchNo As String) As String
        On Error GoTo ToExit '打开错误陷阱
        '------------------------------------------------
        Dim xmlDocum As New DOMDocument
        Dim xmlNodeList As IXMLDOMNodeList
        Dim XMLNode As IXMLDOMNode
        Dim intI As Integer, intJ As Integer    GetXmlValue = ""
        xmlDocum.async = False
        If xmlDocum.Load(App.Path & "\ah.xml") = False Then
            MsgBox "XML载入失败!"
            Set xmlDocum = Nothing
            Exit Function
        End If    Set xmlNodeList = xmlDocum.getElementsByTagName("City")
        For intI = 0 To xmlNodeList.length - 1
            Set XMLNode = xmlNodeList.Item(intI)
            If XMLNode.Attributes(1).Text = strAreaCode Then
                For intJ = 0 To XMLNode.childNodes.length - 1
                    If XMLNode.childNodes(intJ).Attributes(1).Text = strDispatchNo Then
                        GetXmlValue = XMLNode.Attributes(0).Text & "   " & XMLNode.childNodes(intJ).Attributes(0).Text
                        Exit Function
                    End If
                Next
            End If
        Next    Set xmlNodeList = Nothing
        Set XMLNode = Nothing
        Set xmlDocum = Nothing
        '------------------------------------------------Exit Function    '----------------
    ToExit:
        Resume Next
    End Function