使用 vb 从 xml 中读取特定的单个元素值

read specific single element value from xml using vb

我正在尝试从下方读取特定元素值 xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document Version="7.700000" VersionSeemage="7.7.0.101">
<Server Type="CLitServerModifiable">        
    <CLitInfo>
        <Neutral>
            <GroupInfoPropSet>
                <GroupInfo.List/>
                <GroupInfo.Guid Value="a126be064a25ce3f"/>
            </GroupInfoPropSet>
        </Neutral>
    </CLitInfo>     
</Server>   
</Document>

我只需要这个字符串a126be064a25ce3f。因为我对此很陌生。我尝试了以下代码:

Dim doc As XmlDocument = New XmlDocument()
doc.Load("test.xml")
Dim str As String = doc.SelectSingleNode("DocumentSmg/Server[Type='CLitServerModifiable']/CLitBOMInfo/Neutral/GroupInfoPropSet/GroupInfo.List/GroupInfo.Guid").InnerText
 MsgBox(str) 

它returns没什么

尝试 xml linq :

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 guid As String = CType(doc.Descendants("GroupInfo.Guid").FirstOrDefault().Attribute("Value"), String)

    End Sub
End Module

使用 XElement

    Dim xe As XElement
    xe = XElement.Load("test.xml")
    Dim thisGUID As String = xe...<GroupInfo.Guid>.@Value

为了测试你可以使用文字

    Dim xe As XElement
    '  for testing use a literal
    xe = <Document Version="7.700000" VersionSeemage="7.7.0.101">
             <Server Type="CLitServerModifiable">
                 <CLitInfo>
                     <Neutral>
                         <GroupInfoPropSet>
                             <GroupInfo.List/>
                             <GroupInfo.Guid Value="a126be064a25ce3f"/>
                         </GroupInfoPropSet>
                     </Neutral>
                 </CLitInfo>
             </Server>
         </Document>

    Dim thisGUID As String = xe...<GroupInfo.Guid>.@Value