2017-02-03 6 views
0

私はvb.netからXMLファイルに書きます。サンプルはこのように見えます。XML子ノードの問題を読む

<?xml version="1.0" encoding="utf-8"?> 
    <Settings> 
     <LaunchOnReboot>True</LaunchOnReboot> 
     <SavedMounts> 
     <Mount> 
      <Description>fake mount</Description> 
      <DriveLetter>B</DriveLetter> 
      <DriveLocation>\\Location\1</DriveLocation> 
      <UserName>User</UserName> 
      <Password>FakePassword2</Password> 
      <AutomagicallyMount>False</AutomagicallyMount> 
      <Linux>True</Linux> 
     </Mount> 
     <Mount> 
      <Description>fake mount 2</Description> 
      <DriveLetter>G</DriveLetter> 
      <DriveLocation>\\fake\fakelocation</DriveLocation> 
      <UserName>awiles</UserName> 
      <Password>FakePassword</Password> 
      <AutomagicallyMount>False</AutomagicallyMount> 
      <Linux>True</Linux> 
     </Mount> 
     </SavedMounts> 
    </Settings> 

私はそれを書いても問題はありませんが、私はSavedMounts子ノードを読む際に問題があります。これは私がこれまで行ってきたことですが、特定のElementStringsに基づいて特定の値を引き出す方法がわかりません。

これはコードのように見えますが、少し助けが必要だと思います。

+0

だけでなく、あなただけ表示される文字列。 XMLから値を表示するつもりですが、まずそれを読む必要があります。 –

答えて

1

使用してXML LINQ:messagebox.showで

Imports System.Xml 
Imports System.Xml.Linq 
Module Module1 
    Const FILENAME As String = "c:\temp\test.xml" 
    Sub Main() 
     Dim doc As XDocument = XDocument.Load(FILENAME) 

     Dim results = doc.Descendants("Mount").Select(Function(x) New With { _ 
      .description = CType(x.Element("Description"), String), _ 
      .driveLetter = CType(x.Element("DriveLetter"), String), _ 
      .driveLocation = CType(x.Element("DriveLocation"), String), _ 
      .userName = CType(x.Element("UserName"), String), _ 
      .password = CType(x.Element("Password"), String), _ 
      .automagicallyMount = CType(x.Element("AutomagicallyMount"), String), _ 
      .linux = CType(x.Element("Linux"), Boolean) _ 
     }).ToList() 
    End Sub 

End Module