2017-10-29 15 views
0

このサイトの他のおかげで、元の問題は修正されましたが、新しいものがありました。XML vbのネストされたデータ

パスを表示せずに、コンボボックスに入力するためにブランチの名前フィールドをプルできる必要があります。

が選択され、ボタンをクリックすると、選択したブランチのパスにあるすべてのファイルを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

とは何ですか?何かエラーが出ていますか? – derloopkat

+0

ブランチを取得するにはどうすればいいですか?パスを表示せずに.142をコンボボックスに入れ、次にボタンをクリックするとファイルをパスからusbにコピーします。このプログラムは、ソフトウェアのバージョンを選択し、インストールのためにファイルをUSBにコピーするために使用されますが、各ブランチは別の場所に保存されます。私はそれをより明確にするために質問を編集しました –

+0

あなたからのコードを使用して前の投稿:https://stackoverflow.com/questions/46985365/reading-xml-data-using-linq-multiple-elements-with-the-same-名前-vb – jdweng

答えて

0

"GapGunソフトウェアバージョン7.1"を選択し、 ".142"を選択して、Button1をクリックすると、パスが取得されます。それはあなたが後でそれを見つけるために別のクエリを必要としないので、プロパティの値で何かを置くために便利ですので、TextValue

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 

コンボボックスの項目は、2つのプロパティを持っています。 ComboBox1_SelectedIndexChangedでは、ブランチ名をテキストとして、各項目の値としてのパスをComboBox2としました。

xelementも複数のメソッドで使用されていて、同じファイルを何度も解析していたため、クラスのメンバとして移動しました。

+0

すごく、ありがとう!私の問題を解決した今、私の問題はちょっとしたことでうまくいく! –

関連する問題