Question LINQ to XML help

cossie

Member
Joined
Jul 3, 2008
Messages
6
Programming Experience
1-3
Hi,

I went through Beth Massi's LINQ to XML tutorial, where she queries a Customers.xml file (if any of you know it). I saved an Atom feed as an XML file on my computer and I tried querying it in a similar fashion but it never returns anything. Please excuse the formatting, I've tried to tidy it up but it's just not working:mad:

VB.NET:
Dim myAtom = XDocument.Load("C:\atom.xml")
Dim atomQuery = <atomQuery>
                       <%= From aq In myAtom...<entry> _
                             Select aq %>
                        </atomQuery>
' This line is from her tutorial
My.Computer.FileSystem.WriteAllText("atomQuery.xml", atomQuery.ToString, False)
' As is this one
Process.Start("atomQuery.xml")

I'm kind of confused, because I saved the BBC News' RSS feed to an XML file and I am able to query it perfectly fine using the almost the same code as above (except I query for the item node and not the entry node).

VB.NET:
Dim myNews = XDocument.Load("C:\bbc.xml")

Dim items = <items>
    <%= From n In myNews...<item> _
           Select n %>
               </items>
...
...

Can anyone see what I'm doing wrong in my query?

Thanks :)

Cossie
 
Atom feed is defined with a default document namespace, without using this namespace your code can't see the nodes. The default namespace for Xml can be imported like this:
VB.NET:
Imports <xmlns="http://www.w3.org/2005/Atom">
This would mean all your Linq-Xml code would use this as namespace for node references, unless other namespace was specified. This could be a very unwanted side effect.

Since there can only be one default namespace one often uses prefixes for various namespaces, it also solved the problem with not always wanting to use a default namespace. The prefix is then a named shortcut to that namespace and you use the appropriate prefix when addressing various nodes. Example import:
VB.NET:
Imports <xmlns:pre="http://www.w3.org/2005/Atom">
In the Linq query you would then change <entry> to <pre:entry> to address the entry node in the namespace defined by the "pre" prefix.



A third way to use namespaces is without using imports, only declarations like this:
VB.NET:
Dim ns As XNamespace = "http://www.w3.org/2005/Atom"
Dim entryname As XName = ns + "entry"
Dim atomQuery = <atomQuery>
                    <%= From aq In myAtom.Descendants(entryname) _
                        Select aq %>
                </atomQuery>
You can similarly define default namespace and prefixed namespaces in root element of output document to prevent the node namespaces being expanded for each child elemtent. Like this:
VB.NET:
<atomQuery xmlns="http://www.w3.org/2005/Atom">
and this:
VB.NET:
<atomQuery xmlns:pre="http://www.w3.org/2005/Atom">
 
Thanks a milion for such a complete reply, JohnH :)

I tried to use this namespace
VB.NET:
"http://www.w3.org/2005/Atom"
as you suggested, but it never returned anything. When the XML file opens it simply has the tag
VB.NET:
<atomQuery/>
and nothing else.

However, after I read up further on Atom I came across this
VB.NET:
"http://purl.org/atom/ns#"
and it works fine - weird!

(PS I tried to add to your reputation, but it says I need to spread it around, sorry)
 
Thanks, you should be able to read what namespaces are in use from source Xml document. The one I used is for Atom version 1.0 feed, yours is Atom version 0.3.
 
Back
Top