避免在这个函数上递归关联XML [英] Avoid recursion on this function XML related

查看:96
本文介绍了避免在这个函数上递归关联XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此函数检索XML文档的XPath:

This function retrieves the XPaths of an XML document:

''' <summary>
''' Gets all the XPath expressions of an <see cref="Xml.XmlDocument"/> document.
''' </summary>
''' <param name="Document">Indicates the <see cref="Xml.XmlDocument"/> object.</param>
''' <returns>List(Of System.String).</returns>
Public Function GetXmlXPaths(ByVal Document As Xml.XmlDocument) As List(Of String)

    Dim XPathList As New List(Of String)

    Dim XPath As String = String.Empty

    For Each Child As Xml.XmlNode In Document.ChildNodes

        If Child.NodeType = Xml.XmlNodeType.Element Then
            GetXmlXPaths(Child, XPathList, XPath)
        End If

    Next ' child

    Return XPathList

End Function

''' <summary>
''' Gets all the XPath expressions of an <see cref="Xml.XmlNode"/>.
''' </summary>
''' <param name="Node">Indicates the <see cref="Xml.XmlNode"/>.</param>
''' <param name="XPathList">Indicates a ByReffered XPath list as a <see cref="List(Of String)"/>.</param>
''' <param name="XPath">Indicates the current XPath.</param>
Private Sub GetXmlXPaths(ByVal Node As Xml.XmlNode,
                         ByRef XPathList As List(Of String),
                         Optional ByVal XPath As String = Nothing)

    XPath &= "/" & Node.Name

    If Not XPathList.Contains(XPath) Then
        XPathList.Add(XPath)
    End If

    For Each Child As Xml.XmlNode In Node.ChildNodes

        If Child.NodeType = Xml.XmlNodeType.Element Then
            GetXmlXPaths(Child, XPathList, XPath)
        End If

    Next ' child

End Sub

这是一个用法示例:
Dim xml As New Xml.XmlDocument
xml.LoadXml()

This is an usage example:

Private Sub Test() 

    Dim xml As New Xml.XmlDocument
    xml.LoadXml((<?xml version="1.0" encoding="Windows-1252"?>
                 <!--XML Songs Database-->
                 <Songs>
                     <Song><Name>My Song 1.mp3</Name></Song>
                     <Song><Name>My Song 2.ogg</Name></Song>
                     <Song><Name>My Song 3.wav</Name></Song>
                 </Songs>).ToString)

    Dim xPathList As List(Of String) = GetXmlXPaths(xml)

    ListBox1.Items.AddRange(xPathList.ToArray)


End Sub

我想改进函数以避免递归同时I然后实现一个Iterator:

I would like to improve the function to avoid the recursion at the same time I implement an Iterator then:

Public Iterator Function GetXmlXPaths(ByVal document As Xml.XmlDocument) _
As IEnumerable(Of String)

    Yield ...

End Function



How I could do it?.

推荐答案

您可以尝试使用XmlReader的Read函数。这将逐个读取所有节点。然后它只是将前面的节点存储在列表中。

You can try using the Read function of the XmlReader. This will read all nodes one by one. Then it's just a about storing the previous nodes in a list. I don't know how well this will work with bigger xml.

    Dim xml As New Xml.XmlDocument
    xml.LoadXml((<?xml version="1.0" encoding="Windows-1252"?>
                 <!--XML Songs Database-->
                 <Songs>
                     <Song><Name>My Song 1.mp3</Name></Song>
                     <Song><Name>My Song 2.ogg</Name></Song>
                     <Song><Name>My Song 3.wav</Name></Song>
                 </Songs>).ToString)

    Dim xPathList As New List(Of String)

    Dim reader As XmlReader = xml.CreateNavigator.ReadSubtree
    Dim curNodes As New List(Of String)
    Dim curPath As String

    While reader.Read()
        If reader.NodeType = XmlNodeType.Element Then
            If curNodes.Count <= reader.Depth Then
                curNodes.Add(reader.Name)
            Else
                curNodes(reader.Depth) = reader.Name
            End If

            curPath = String.Join("/", curNodes.ToArray(), 0, reader.Depth + 1)

            If Not xPathList.Contains(curPath) Then
                xPathList.Add(curPath)
            End If
        End If
    End While

    For Each s As String In xPathList
        Console.WriteLine(s)
    Next

这篇关于避免在这个函数上递归关联XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆