2

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">

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)

1 Answer 1

2

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">

Then you can query with:

xdoc.Root.<ns:Child>.@Name
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.