且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据节点拆分 Xml 文档

更新时间:2023-01-11 15:58:14

最后我成功根据节点拆分xml ..我已经根据xml中的节点保存了单独的kml文件..这是我的解决方案

Finally i made success for splitting xml according to node ..I have saved individual kml files according to node in xml ..Here is my solution

Public Sub SplitXml(ByVal XmlDoc As XmlDocument, ByVal SaveLocation As String)

        Dim TmpXml As XmlDocument = XmlDoc
        Dim Str As String = "<?xml version=""1.0"" encoding=""UTF-8""?>" & "<kml xmlns=" & Chr(34) & "http://www.opengis.net/kml/2.2" & Chr(34) & ">" & "<Document>"
        Dim DocumentNodes As XmlNodeList = TmpXml.GetElementsByTagName("Document")
        '=======================
        'Building Common String 
        '=======================
        For Each node As XmlNode In DocumentNodes
            Dim DocumentChildNodes As XmlNodeList = node.ChildNodes
            For Each Childnode As XmlNode In DocumentChildNodes
                If Childnode.Name <> "Folder" Then
                    Str = Str & Childnode.OuterXml.Replace("xmlns=""http://www.opengis.net/kml/2.2""", "")
                Else
                    Exit For
                End If
            Next
        Next

        Dim FolderNodes As XmlNodeList = TmpXml.GetElementsByTagName("Folder")
        Dim FolderName As String = String.Empty
        Dim XmlDocSave As XmlDocument = New XmlDocument()
        Dim StrXml As String = String.Empty
        Dim TmpStr As String = String.Empty
        Dim FileName As String = String.Empty
        For Each node As XmlNode In FolderNodes
            '==============================================================
            'Creating Directories For kml Getting Name from FirstChild Node
            '===============================================================
            FolderName = DirectCast(DirectCast(node, System.Xml.XmlElement).FirstChild, System.Xml.XmlElement).InnerText
            FolderName = FolderName.Replace(".", "_")
            FolderName = FolderName.Replace(" ", "")
            If (Not System.IO.Directory.Exists(SaveLocation & "" & FolderName)) Then
                System.IO.Directory.CreateDirectory(SaveLocation & "" & FolderName)
            End If
            '==============================================================
            'Creating kml Files Getting Name from FirstChild Node
            '===============================================================
            Dim FolderChildNodes As XmlNodeList = node.ChildNodes
            For Each childnode As XmlNode In FolderChildNodes
                If childnode.Name = "Placemark" Then
                    FileName = DirectCast(DirectCast(childnode, System.Xml.XmlElement).FirstChild, System.Xml.XmlElement).InnerText
                    FileName = FileName.Replace(".", "_")
                    FileName = FileName.Replace(" ", "")
                    StrXml = Str & "<Folder>" & TmpStr & childnode.OuterXml & "</Folder>" & "</Document>" & "</kml>"

                    XmlDocSave.LoadXml(StrXml)
                    XmlDocSave.Save(SaveLocation & "" & FolderName & "" & FileName & ".kml")
                    XmlDocSave = New XmlDocument()
                    StrXml = String.Empty
                Else
                    TmpStr = TmpStr & childnode.OuterXml
                End If
            Next
            TmpStr = String.Empty
        Next
    End Sub