2017-05-01 4 views
0

私は数日間この作業を行う方法を工夫しようとしています(同じ記事をもう一度見て回ってしまいました。要素vb.net内の単一のxml属性から値を収集

私は最後の1が正しい方向についてのラウンドで私を指すように見えたのRead XML in VB.netHow to read an XML FileHow to read XML elements in VB.NETRetrieve single attribute value from an xml doc elementを見てきましたが、で指定されたコードを実行している時に、質問のググので、多くの組み合わせをVE) DWRoelands私は "System.Collections.Generic.List`1 [System.String]"をコンソール出力に受け取ります。

XMLを参照すると、/ Files/File/Link要素のHREF属性からdescriptor.ovfとしてリストされたファイルのみ()を抽出する必要があります(これを実行する2番目のXMLファイルには、 VMDK)

ありがとう

XMLスニペット:

<?xml version="1.0" encoding="UTF-8"?> 
 
<VApp href="https://#####/api/vApp/vapp-##"> 
 
    <Link rel="down"/> 
 
    <Description>Test vAPP</Description> 
 
    <Tasks> 
 
     <Task cancelRequested="false" > 
 
      <Link rel="task:cancel" /> 
 
      <Progress>1</Progress> 
 
      <Details/> 
 
     </Task> 
 
    </Tasks> 
 
    <Files> 
 
     <File bytesTransferred="0" size="-1" name="descriptor.ovf"> 
 
      <Link rel="upload:default" href="https://server/transfer/descriptor.ovf"/> 
 
     </File> 
 
    </Files> 
 
    <InMaintenanceMode>false</InMaintenanceMode> 
 
</VApp>

全体のXML:

<?xml version="1.0" encoding="UTF-8"?> 
 
<VApp xmlns="http://www.vmware.com/vcloud/v1.5" ovfDescriptorUploaded="false" deployed="false" status="0" name="Test vAPP" id="urn:vcloud:vapp:#####" href="https://server/api/vApp/vapp-#####" type="application/vnd.vmware.vcloud.vApp+xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 https://server/api/v1.5/schema/master.xsd"> 
 
    <Link rel="down" href="https://server/api/vApp/vapp-#####/controlAccess/" type="application/vnd.vmware.vcloud.controlAccess+xml"/> 
 
    <Link rel="up" href="https://server/api/vdc/#####" type="application/vnd.vmware.vcloud.vdc+xml"/> 
 
    <Link rel="down" href="https://server/api/vApp/vapp-#####/owner" type="application/vnd.vmware.vcloud.owner+xml"/> 
 
    <Link rel="down" href="https://server/api/vApp/vapp-#####/metadata" type="application/vnd.vmware.vcloud.metadata+xml"/> 
 
    <Link rel="ovf" href="https://server/api/vApp/vapp-#####/ovf" type="text/xml"/> 
 
    <Link rel="down" href="https://server/api/vApp/vapp-#####/productSections/" type="application/vnd.vmware.vcloud.productSections+xml"/> 
 
    <Description>Test vAPP</Description> 
 
    <Tasks> 
 
     <Task cancelRequested="false" expiryTime="2017-07-30T11:02:07.457+08:00" operation="Importing Virtual Application Test vAPP(#####)" operationName="vdcUploadOvfContents" serviceNamespace="com.vmware.vcloud" startTime="2017-05-01T11:02:07.457+08:00" status="running" name="task" id="urn:vcloud:task:######" href="https://server/api/task/####" type="application/vnd.vmware.vcloud.task+xml"> 
 
      <Link rel="task:cancel" href="https://server/api/task/#####/action/cancel"/> 
 
      <Owner href="https://server/api/vApp/vapp-#####" name="Test vAPP" type="application/vnd.vmware.vcloud.vApp+xml"/> 
 
      <User href="https://server/api/admin/user/####" name="#####" type="application/vnd.vmware.admin.user+xml"/> 
 
      <Organization href="https://server/api/org/#####" name="#####" type="application/vnd.vmware.vcloud.org+xml"/> 
 
      <Progress>1</Progress> 
 
      <Details/> 
 
     </Task> 
 
    </Tasks> 
 
    <Files> 
 
     <File bytesTransferred="0" size="-1" name="descriptor.ovf"> 
 
      <Link rel="upload:default" href="https://server/transfer/#####/descriptor.ovf"/> 
 
     </File> 
 
    </Files> 
 
    <DateCreated>2017-05-01T11:02:07.417+08:00</DateCreated> 
 
    <Owner type="application/vnd.vmware.vcloud.owner+xml"> 
 
     <User href="https://server/api/admin/user/#####" name="#####" type="application/vnd.vmware.admin.user+xml"/> 
 
    </Owner> 
 
    <InMaintenanceMode>false</InMaintenanceMode> 
 
</VApp>

答えて

0

まず、あなたのファイルをロードし、あなたがXElementオブジェクトを使用する場合は、テストするためにリテラルを使用することができます。私のようにこれを追加する必要がありました、上から全体のXMLの例に基づいて編集したあなたは

をしたい要素

Dim dovf As IEnumerable = (From el In xe...<Files> 
           Where el.<File>[email protected] = "descriptor.ovf" 
           Select el.<File>.<Link>[email protected]) 

    For Each s As String In dovf 
     Stop 
    Next 

を選択するのは容易であるロードされていることを今

'Dim yourpath As String = "your path here" 
    Dim xe As XElement 
    'to load from a file 
    'xe = XElement.Load(yourpath) 

    'for testing 
    xe = <VApp href="https://#####/api/vApp/vapp-##"> 
      <Link rel="down"/> 
      <Description>Test vAPP</Description> 
      <Tasks> 
       <Task cancelRequested="false"> 
        <Link rel="task:cancel"/> 
        <Progress>1</Progress> 
        <Details/> 
       </Task> 
      </Tasks> 
      <Files> 
       <File bytesTransferred="0" size="-1" name="descriptor.ovf"> 
        <Link rel="upload:default" href="https://server/transfer/descriptor.ovf"/> 
       </File> 
      </Files> 
      <InMaintenanceMode>false</InMaintenanceMode> 
     </VApp> 


    ' to save file 
    ' xe.Save(yourpath) 

プログラムの最初の行

Imports <xmlns="http://www.vmware.com/vcloud/v1.5"> 
+0

こんにちはdbasnett、 お返事ありがとうございます。 私はコードをテストしましたが、私が必要としていることをするように見えましたが、以前のXML(xmlを減らしたもの)をXMLに置き換えた場合、dovf変数を記述すると、コンソールに接続します。 実際のコードを含めるように投稿を更新しました(機密情報がハッシュアウトされています) どこが間違っているのか正しい方向に向けることができますか? – SuperTrollMan

+0

@SuperTrollMan - 編集を参照してください。 – dbasnett

0

Paste your xml as classes with Visual Studio。スキーマが正しく生成されるように、xml配列に複数の項目があることを確認してください。結果は次のとおりです。

'''<remarks/> 
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True), _ 
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _ 
Partial Public Class VApp 

    Private linkField As VAppLink 

    Private descriptionField As String 

    Private tasksField() As VAppTask 

    Private filesField() As VAppFile 

    Private inMaintenanceModeField As Boolean 

    Private hrefField As String 

    '''<remarks/> 
    Public Property Link() As VAppLink 
     Get 
      Return Me.linkField 
     End Get 
     Set(value As VAppLink) 
      Me.linkField = value 
     End Set 
    End Property 

    '''<remarks/> 
    Public Property Description() As String 
     Get 
      Return Me.descriptionField 
     End Get 
     Set(value As String) 
      Me.descriptionField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlArrayItemAttribute("Task", IsNullable:=False)> _ 
    Public Property Tasks() As VAppTask() 
     Get 
      Return Me.tasksField 
     End Get 
     Set(value As VAppTask()) 
      Me.tasksField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlArrayItemAttribute("File", IsNullable:=False)> _ 
    Public Property Files() As VAppFile() 
     Get 
      Return Me.filesField 
     End Get 
     Set(value As VAppFile()) 
      Me.filesField = value 
     End Set 
    End Property 

    '''<remarks/> 
    Public Property InMaintenanceMode() As Boolean 
     Get 
      Return Me.inMaintenanceModeField 
     End Get 
     Set(value As Boolean) 
      Me.inMaintenanceModeField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property href() As String 
     Get 
      Return Me.hrefField 
     End Get 
     Set(value As String) 
      Me.hrefField = value 
     End Set 
    End Property 
End Class 

'''<remarks/> 
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _ 
Partial Public Class VAppLink 

    Private relField As String 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property rel() As String 
     Get 
      Return Me.relField 
     End Get 
     Set(value As String) 
      Me.relField = value 
     End Set 
    End Property 
End Class 

'''<remarks/> 
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _ 
Partial Public Class VAppTask 

    Private linkField As VAppTaskLink 

    Private progressField As Byte 

    Private detailsField As Object 

    Private cancelRequestedField As Boolean 

    '''<remarks/> 
    Public Property Link() As VAppTaskLink 
     Get 
      Return Me.linkField 
     End Get 
     Set(value As VAppTaskLink) 
      Me.linkField = value 
     End Set 
    End Property 

    '''<remarks/> 
    Public Property Progress() As Byte 
     Get 
      Return Me.progressField 
     End Get 
     Set(value As Byte) 
      Me.progressField = value 
     End Set 
    End Property 

    '''<remarks/> 
    Public Property Details() As Object 
     Get 
      Return Me.detailsField 
     End Get 
     Set(value As Object) 
      Me.detailsField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property cancelRequested() As Boolean 
     Get 
      Return Me.cancelRequestedField 
     End Get 
     Set(value As Boolean) 
      Me.cancelRequestedField = value 
     End Set 
    End Property 
End Class 

'''<remarks/> 
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _ 
Partial Public Class VAppTaskLink 

    Private relField As String 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property rel() As String 
     Get 
      Return Me.relField 
     End Get 
     Set(value As String) 
      Me.relField = value 
     End Set 
    End Property 
End Class 

'''<remarks/> 
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _ 
Partial Public Class VAppFile 

    Private linkField As VAppFileLink 

    Private bytesTransferredField As Byte 

    Private sizeField As SByte 

    Private nameField As String 

    '''<remarks/> 
    Public Property Link() As VAppFileLink 
     Get 
      Return Me.linkField 
     End Get 
     Set(value As VAppFileLink) 
      Me.linkField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property bytesTransferred() As Byte 
     Get 
      Return Me.bytesTransferredField 
     End Get 
     Set(value As Byte) 
      Me.bytesTransferredField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property size() As SByte 
     Get 
      Return Me.sizeField 
     End Get 
     Set(value As SByte) 
      Me.sizeField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property name() As String 
     Get 
      Return Me.nameField 
     End Get 
     Set(value As String) 
      Me.nameField = value 
     End Set 
    End Property 
End Class 

'''<remarks/> 
<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _ 
Partial Public Class VAppFileLink 

    Private relField As String 

    Private hrefField As String 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property rel() As String 
     Get 
      Return Me.relField 
     End Get 
     Set(value As String) 
      Me.relField = value 
     End Set 
    End Property 

    '''<remarks/> 
    <System.Xml.Serialization.XmlAttributeAttribute()> _ 
    Public Property href() As String 
     Get 
      Return Me.hrefField 
     End Get 
     Set(value As String) 
      Me.hrefField = value 
     End Set 
    End Property 
End Class 

これらのクラスにxmlをデシリアライズします。 LINQを使用してname = "descriptor.ovf"の唯一の*要素を探します。必要に応じて例外処理を追加します。

Sub Main() 
    Dim myVApp As VApp 
    Dim serializer As New XmlSerializer(GetType(VApp)) 
    Using sr As New StreamReader("XMLFile1.xml") 
     myVApp = CType(serializer.Deserialize(sr), VApp) 
    End Using 
    Dim href = myVApp.Files.Single(Function(f) f.name = "descriptor.ovf").Link.href 
    Console.WriteLine(href) 
    Console.ReadLine() 
End Sub 

出力:

https://server/transfer/descriptor.ovf

あなただけのアイテム、全ての項目、または最初の項目とを返すためにSingleWhere、またはFirst同じ述語で使用することができます*その名前。

関連する問題