LINQ to XML - Loading an XML file

Edited 2009.11.19 -- Andrew provides valuable information on the topic discussed in this blog.

From: Andrew Baines 
Sent: Thursday, November 19, 2009 12:02 PM
To: bill@global-webnet.com
Subject: Suggestion:LINQ to XML - Loading an XML file
 Hi Bill, in regards to the following blog: http://www.global-webnet.net/blogengine/post/2008/09/12/LINQ-to-XML-Loading-an-XML-file.aspx
 
The issue here is something that I had problems with … the fact that the Newsletter node has the xmlns attribute means that every attribute has a namespace appended to the beginning. Therefore you can’t just ask for the Email element … you have to ask for the namespace + Email element.
 
This can be done like so:
 
string filename = @"d:\projects\newsletterxml\newsletter.xml";
XDocument UsersXML = XDocument.Load(filename);
XNamespace xsi =
"http://tempuri.org/Newsletter.xsd";
var Users = from user in UsersXML.Descendants(xsi + "User")
        
select new
        {
            Validate = user.Element(xsi +
"Validated").Value,
            Name = user.Element(xsi +
"Name").Value,
            Relation = user.Element(xsi +
"Relation").Value,
            Email = user.Element(xsi +
"Email").Value,
            Guid = user.Element(xsi +
"guid").Value
        };



Back before .NET 2.0 I had written Amarillo College Lamplight Youth Theatre a Newsletter program.   A couple of years ago we moved them over to our Community Advanced Starter Kit (CASK) application and simply provided a link to their .NET 1.1 Newsletter.  The only thing preventing us from moving them into the CASKDotNet newsletter is their large contact list and time (we donate our services and maintaining their site can keep us pretty busy at times). 

We're getting ready to release our next version of the CASK (upgrading them) and it seems an appropriate time to get them migrated over.   I have to load their XML Newsletter file and update their SQL Database as applicable to import their contact list.

Since LINQ has dramatically reduced coding with tables and stored procedures, I figured I'd give it a whirl for this XML requirement. Note: I had been avoiding LINQ as long as I could.  When .NET 3.5/Visual Studio SP1 was released I started using the Entity Framework and it was time to roll up my sleeves - wish I had started earlier...

So I broke out the LINQPAD, googled the topic and found an excellent article from Scott Guthrie that cover's the topic nicely HERE so the question was "Why wasn't it working!".  I throught perhaps LINQPAD was broken so I went into Visual Studio 2008 and it yielded the same results - nothing.

So what is wrong with the following picture?

string filename = @"d:\projects\newsletterxml\newsletter.xml";
XDocument UsersXML = XDocument.Load(filename);
var Users = from user in UsersXML.Descendants("User")
        select new
        {
            Validate = user.Element(
"Validated").Value,
            Name = user.Element(
"Name").Value,
            Relation = user.Element(
"Relation").Value,
            Email = user.Element(
"Email").Value,
            Guid = user.Element(
"guid").Value
        };

It was the .XSD statement!  I didn't need it for the migration tool so I didn't pull it into the project.  To get my query working I simply had to remove the xmlns .XSD attribute from the Newsletter element and walla! I had my list.   I guess now you can see why I wished I had started using LINQ earlier!


Tags: , ,
Categories:


Actions: E-mail | Permalink |  Grammar/Typo/Better way? Please let me know