I have an XML document that I only need to get 2 values from. In the past, I have been able to do this very easily using an XDocument:
Example XML:
<?xml version="1.0" standalone="yes"?>
<Vehicles>
<Truck>
<Color>Blue</Color>
<Make>General Motors</Make>
<Weight>3000</Weight>
</Truck>
</Vehicles>
If I wanted to access just the <Weight>
of the <Truck>
, I could do by:
Dim xdoc as XDocument = XDocument.Load("c:/example.xml")
Dim truckWeight as Integer = Integer.Parse(xdoc.<Vehicles>.<Truck>.<Weight>.Value)
...and I would be on my merry way. However, in this case, my XML document has a namespace at the beginning, like so:
<?xml version="1.0" standalone="yes"?>
<Vehicles xmlns="http://interweb.com/Vehicles.xsd">
<Truck>
<Color>Blue</Color>
<Make>General Motors</Make>
<Weight>3000</Weight>
</Truck>
</Vehicles>
If I attempt to use the above lines to get truckWeight
, .Value
returns Nothing
even though xdoc
would appear to be populated using Visual Studio's text reader.
What can I do to be able to use the mentioned XDocument notation I have used previously while leaving the XML file unaltered? If this is not possible, what is the alternate way of accessing something like <Weight>
in an XML file with a namespace?