【问题标题】:XML Nested data for vbvb 的 XML 嵌套数据
【发布时间】:2017-10-29 09:19:09
【问题描述】:

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

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

一旦选择并单击按钮,它必须将所选分支路径下的所有文件复制到 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。

【问题讨论】:

  • 还有什么问题?你有什么错误吗?
  • 我如何获得分支,例如.142 进入组合框而不显示路径,然后当我单击按钮时,将文件从路径复制到 USB。该程序将用于选择软件版本并将文件复制到 USB 进行安装,但每个分支存储在不同的位置。我已经编辑了问题以使其更清晰
  • 使用您之前发布的代码:stackoverflow.com/questions/46985365/…
  • 我有一个适用于分支但我不知道如何使用路径
  • 分支有两种格式。哪一个是正确的?一个有名称和路径,一个没有名称或路径但有文本?另外,这似乎是winforms,为什么里面有Console.WriteLine() 调用?您是从其他地方复制/粘贴这些行吗?

标签: xml vb.net linq


【解决方案1】:

选择“GapGun Software Version 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 移动为该类的成员,因为它被多个方法使用并且多次​​解析同一个文件。

【讨论】:

  • 太好了,非常感谢!解决了我的问题,现在我的问题只需稍加修改就可以很好地解决!
猜你喜欢
  • 2011-02-07
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 2018-06-10
  • 2017-08-28
  • 2020-12-29
  • 1970-01-01
  • 2019-08-28
相关资源
最近更新 更多