Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read XML elements in VB.NET

Tags:

xml

vb.net

I have a very simple problem, but since I am new to XML, I face some problems. I have this XML document:

<?xml version="1.0" encoding="utf-8"?>  
<Form_Layout>
  <Location>
    <LocX>100</LocX>
    <LocY>100</LocY>  
  </Location>  
  <Size>  
    <Width>300</Width>  
    <Height>300</Height>  
  </Size>  
</Form_Layout> 

What I want to do is read the values from the LocX, LoxY, Width, and Height elements into my corresponding variables.

Here is what I have tried:

Dim XmlReader = New XmlNodeReader(xmlDoc)  
While XmlReader.Read  
    Select Case XmlReader.Name.ToString()  
        Case "Location"  
            If XmlReader.??  
        Case "Size"  
            If XmlReader.??
    End Select  
End While  

But, I cannot figure out how to access each child Node.

like image 269
Nianios Avatar asked Oct 16 '25 19:10

Nianios


1 Answers

If you're able to use Linq to XML, you can use VB's XML Axis Properties:

Dim root As XElement = XDocument.Load(fileName).Root

Dim LocX = Integer.Parse(root.<Location>.<LocX>.Value)
Dim LocY = Integer.Parse(root.<Location>.<LocY>.Value)

And root.<Location>.<LocY>.Value = CStr(120) works too.

like image 183
Mark Hurd Avatar answered Oct 18 '25 11:10

Mark Hurd