views:

16

answers:

1

I'm trying to grasp the linq to xml 'inline query syntax' features of VB.Net

First I tried with this simple xml file:

    <?xml version="1.0" encoding="utf-8" ?>
    <Root>
       <Child Name="somename">
          <SomeAttribute>SomeValue</SomeAttribute>
       </Child>
    </Root>

This xml, when loaded in an XDocument, can be loaded and queried as follows:

    Dim xdoc = XDocument.Load("sample.xml")
    Console.WriteLine(xml.Root.<Child>.@Name)

Then I change the <Root> element in the sample xml file to:

    <Root xmlns="http://SomeNamespace"&gt;

Now I can't seem to use the convenient 'Axis Properties' syntax anymore... I can only get it to work with the explicit XElement syntax:

    Dim ns As XNamespace = "http://SomeNamespace"
    ' works, but I would like to use the same syntax as above...
    Console.WriteLine(xdoc.Descendants(ns + "Child").First().Attribute("Name").Value)
A: 

I found the answer here

At first, I didn't know this syntactic feature was called "Axis Properties".

I had to add an Imports statement for the xml namespace:

Imports <xmlns:ns="http://SomeNamespace"&gt;

Then you can query with:

xdoc.Root.<ns:Child>.@Name
jeroenh