开发者

vb.net linq to xml syntax for documents with xml namespace

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")
    Co开发者_如何学运维nsole.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)


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜