Search code examples
xmlvb.netreadxml

read specific single element value from xml using vb


I am trying to read specific element value from below 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>

I need only this string a126be064a25ce3f. As I am very new to this. I tried below code:

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) 

It returns nothing


Solution

  • Try 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