Compartilhar via


When using LINQ to XML why don’t I get results if I don’t import a XML namespace?

Hi,

I posted a bit of code the other day which I used to get a list of all Code Snippets we ship in Visual Studio. In a nutshell, I used XElement.Load to create a new XML document from a filename from which I then read elements from (There are way to many ”from” in this sentence ;-) ). Unfortunately, I forgot to import the XML Namespace in the code I posted which caused my code not to return any results (but instead provided me with a brilliant opportunity to write another post. hehe :-) ).

Let’s have a look at the code which is causing trouble and a Code Snippets to understand what’s going on (you can find the complete code including the Snippet class in the aforementioned post):

Dim query = _

    From file In My.Computer.FileSystem.GetFiles( _

        "C:\Program Files\Microsoft Visual Studio 10.0", _

        FileIO.SearchOption.SearchAllSubDirectories) _

    Where file.EndsWith(".snippet") _

    Order By file

 

Dim snippets As New List(Of Snippet)

Dim snippetDocument As XElement

Dim snippet As Snippet

 

For Each item In query

    snippetDocument = XElement.Load(item)

    If snippetDocument...<Title>.Value IsNot Nothing Then

 

        snippet = New Snippet With {.Title = snippetDocument...<Title>.Value.ToString _

                                    , .Description = snippetDocument...<Description>.Value.ToString _

  , .Path = item _

                                    , .Size = New System.IO.FileInfo(item).Length}

        snippets.Add(snippet)

    End If

Next

  Code Snippet

<?xml version="1.0" encoding="utf-8"?>

<CodeSnippets xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

  <CodeSnippet Format="1.0.0">

    <Header>

      <Title>Activate a Running Application by Name</Title>

      <Author>Microsoft Corporation</Author>

      <Description>Activates a running application using the name of the application.</Description>

      <Shortcut>appActNa</Shortcut>

    </Header>

    <Snippet>

      <Imports>

        <Import>

          <Namespace>Microsoft.VisualBasic</Namespace>

        </Import>

      </Imports>

      <Declarations>

        <Literal>

          <ID>applicationName</ID>

          <Type>String</Type>

          <ToolTip>Replace with the name of the application. This is often the title of the application window.</ToolTip>

          <Default>"Untitled - Notepad"</Default>

        </Literal>

      </Declarations>

      <Code Language="VB" Kind="method body"><![CDATA[AppActivate($applicationName$)]]></Code>

    </Snippet>

  </CodeSnippet>

</CodeSnippets>

As you can see there is a <Title> element in the the Code Snippet to which we are referring to in the code above… or are we?

Let’s dig a bit deeper, open the compiled program in .NET Reflector and disassemble it (if you don’t have .NET Reflector you have to stop reading NOW and get it!! Seriously, this tool is a life-safer and I’ve learned soooo much using it!).

If you focus on the parts I highlighted in red in the above picture you can see that <Title> consists out of the XML name and the XML namespace (check out XName..::.Get Method (String, String)). Since I didn’t import the namespace in my program an empty namespace got used resulting in the qualified name Title which doesn’t fit the namespace of the Code Snippet {https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet}Title, hence my program didn’t return any results.

To solve this problem I added the following line of code to my program

Imports <xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

and – finally – all is good :-)

Hopefully this solved the mystery of the missing results ;-)

Best!

    Daniel

Comments