用于vb的XML嵌套数据

时间:2017-10-29 09:19:10

标签: xml vb.net linq

感谢本网站其他地方的帮助,我修复了原来的问题,但有一个新问题。

我需要能够拉出分支的名称字段以填充组合框,而不显示路径。

选择并单击按钮后,必须将所选分支路径下的所有文件复制到USB。

<?xml version="1.0" encoding="UTF-8"?>
<Versions>
    <Version>
        <Trunk>GapGun Software Version 7.1</Trunk>
            <Branch Name=".142" Path="\\MEDIA-SERVER\Plex"/>
            <Branch>.145</Branch>
            <Branch>.148</Branch>
            <Branch>.153</Branch>
            <Branch>.176</Branch>
    </Version>
    <Version>
        <Trunk>GapGun Software Version 7.2</Trunk>
            <Branch>.152</Branch>
            <Branch>.155</Branch>
            <Branch>.158</Branch>
            <Branch>.163</Branch>
            <Branch>.166</Branch>
    </Version>
</Versions> 


Imports System.IO

Public Class Form1
    Private Response As Object

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim xelement As XElement = XElement.Load("F:\Test.xml")

        Dim Versions As IEnumerable(Of XElement) = xelement.Elements()

        For Each Version In Versions
            Console.WriteLine(Version.Element("Trunk").Value)
            ComboBox1.Items.Add(Version.Element("Trunk").Value)
        Next Version

        ComboBox3.Items.AddRange(System.IO.Directory.GetLogicalDrives)

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox2.Items.Clear()
        ComboBox2.Text = ""
        Dim xelement As XElement = XElement.Load("F:\Test.xml")
        Dim name =
        From nm In xelement.Elements("Version")
        Where CStr(nm.Element("Trunk")) = ComboBox1.Text
        Select nm

        For Each xEle As XElement In name
            Dim branches = xEle.Elements("Branch").Select(Function(el) el.Value).ToArray()

            Console.WriteLine(xEle)
            ComboBox2.Items.AddRange(branches)
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    End Sub
End Class

just for reference

如果需要重建xml,我也没有问题。目前只需要一个沙箱。

1 个答案:

答案 0 :(得分:1)

选择“GapGun软件版本7.1”然后选择“.142”,单击Button1,您将获得路径。

Imports System.IO

Public Class Form1
    Private Response As Object
    Private xelement As XElement = XElement.Load("F:\Test.xml")

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Versions As IEnumerable(Of XElement) = xelement.Elements()
        For Each Version In Versions
            Console.WriteLine(Version.Element("Trunk").Value)
            ComboBox1.Items.Add(Version.Element("Trunk").Value)
        Next Version

        ComboBox3.Items.AddRange(System.IO.Directory.GetLogicalDrives)

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox2.Items.Clear()
        ComboBox2.Text = ""
        Dim name =
        From nm In xelement.Elements("Version")
        Where CStr(nm.Element("Trunk")) = ComboBox1.Text
        Select nm

        For Each xEle As XElement In name
            Dim branches = xEle.Elements("Branch").ToDictionary( _
                   Function(k) If(String.IsNullOrEmpty(k.Value), k.Attribute("Name").Value, k.Value), _
                   Function(v) If(v.Attribute("Path") Is Nothing, "", v.Attribute("Path").Value))

            Console.WriteLine(xEle)
            ComboBox2.DataSource = New BindingSource(branches, Nothing)
            ComboBox2.DisplayMember = "Key"
            ComboBox2.ValueMember = "Value"
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim name = ComboBox2.SelectedText
        Dim path = ComboBox2.SelectedValue
        If Not path = "" Then
            MessageBox.Show(path)
        End If
    End Sub
End Class

组合框项目有两个属性:TextValue,因此将某些内容放入属性值非常方便,因此您不需要其他查询来查找它。在ComboBox1_SelectedIndexChanged中,我将分支名称作为文本,将路径作为ComboBox2的每个项目的值。

还将xelement作为类的成员移动,因为它被多个方法使用,并且它多次解析同一个文件。