搞鼓半天弄出来的查找 XML 节点的 VB.net 2005 代码,找大侠优化
Private Const OneNode As String = "CK"
''' <summary>
''' 查找xml文档子级节点的下一节点是否存在
''' </summary>
''' <param name="XmlPath">xml路径</param>
''' <param name="Node">节点</param>
''' <param name="ChildNode" >子节点</param>
''' <param name="ChildsNode" >下一子节点</param>
''' <param name="ChildesNode" >下一子节点</param>
Public Shared Function ReadXmlReturnNode(ByVal Node As String, Optional ByVal ChildNode As String = "", Optional ByVal ChildsNode As String = "", Optional ByVal ChildesNode As String = "", Optional ByVal XmlPath As String = "CKs.xml") As Integer
If Trim(Node) = "" Then
Return 0
Else
Node = Node.ToUpper.Trim
End If
Dim docXml As New XmlDocument()
docXml.Load(XmlPath)
If docXml.DocumentElement.Name = OneNode Then '查看文件是否正确
Dim v1, v2, v3 As Boolean
If Not Trim(ChildNode) = "" Then
v1 = True
ChildNode = ChildNode.ToUpper.Trim
If Not Trim(ChildsNode) = "" Then
v2 = True
ChildsNode = ChildsNode.ToUpper.Trim
If Not Trim(ChildesNode) = "" Then
v3 = True
ChildesNode = ChildesNode.ToUpper.Trim
End If
End If
End If
For Each ThisRec As XmlNode In docXml.DocumentElement
If ThisRec.Name = Node Then '查看符合要求的第一个子节点
If v1 Then
For Each Child As XmlNode In ThisRec
If Child.Name = ChildNode Then '查看符合要求的第二个子节点
If v2 Then
For Each Childs As XmlNode In Child
If Childs.Name = ChildsNode Then '查看符合要求的第三个子节点
If v3 Then
For Each Childes As XmlNode In Childs
If Childes.Name = ChildesNode Then '查看符合要求的第四个子节点
Return 4
End If
Next
Else
Return 3
End If
End If
Next
Else
Return 2
End If
End If
Next
Else
Return 1
End If
End If
Next
End If
Return 0 '任一不符合均返回 0
End Function
-------------这段段码感觉太长了点,会不会性能上稍弱了点,如果会,希望有大侠指点下-------------