Read XML Attribute using XDoc and

Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 238
Suppose we have a XML file (say input.xml ) as under

<configuration><configValues first='true' second='false' third='12/29/2016' fourth='this is 4th value'/></configuration>

The objectiveis to read the attribute values. Below is the way

Imports System.Xml.Linq

Namespace ConsoleApplication1
Class Program
Private Shared Sub Main(args As String())
'the xml string
Dim xmlDocPath As String = "C:\Users\niladri.biswas\Desktop\dotnetfunda\2016 Articles\May2016\input.xml"

'load the xml string in XDocument
Dim doc As XDocument = XDocument.Load(xmlDocPath)

'read the values
For Each element As var In doc.Descendants("configValues")
Console.WriteLine("{0} = {1}", "first", element.Attribute("first").Value)
Console.WriteLine("{0} = {1}", "second", element.Attribute("second").Value)
Console.WriteLine("{0} = {1}", "third", element.Attribute("third").Value)
Console.WriteLine("{0} = {1}", "fourth", element.Attribute("fourth").Value)
End Sub
End Class
End Namespace


first  =  true
second = false
third = 12/29/2016
fourth = this is 4th value

Comments or Responses

Login to post response