The little ‘gotcha’ in LINQ to XML

ASP.Net No Comments »

Gotcha in LINQ to XML

I have been working with LINQ to XML for the last couple days working on importing some XML data into a database.

I have used LINQ before with very little effort which is why I decided to give it another go for this project.

I got my program all fleshed out and started writing the LINQ query. The XML file was very simple just a root element with a collection of children.

<root>
	<child />
	<child />
	<child />
	<child />
</root>

So when my query kept returning 0 elements I started to get frustrated. Why wasn’t it working? I pulled up the past project I had done using LINQ and compared the queries. They were identical from a syntax standpoint except the new query wasn’t returning anything.

After a few short Google searches I discovered it was because the new query was reading an XML file that was using namespaces.

<root xmlns="http://namespace.uri">
	<x:child xmlns:x="http://x.namespace.uri"/>
	<x:child />
	<x:child />
	<x:child />
</root>

In this case I was able to remove the namespaces as they were not needed and had been automatically add when I downloaded the file from SharePoint.

After removing the namespaces the query worked beautifully as I had originally expected it to.

The long and short of this is watch out for namespaces when you are writing queries for LINQ to XML.

I didn’t take the time to find a solution that left the namespaces in place because I didn’t really have the time to waste if it wasn’t necessary. If anyone can provide some advice or a solution I would appreciate it. Thanks.

Popularity: 8% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.

Importing Assemblies Into C#

ASP.Net No Comments »

Sometimes it is necessary to use an assembly that was not created in .Net. This usually involves doing some low level things that are not native to C# or .Net and require you to import an existing assembly, perhaps a Win32 assembly, or to write your code in C++ and then import that into C#.

Let’s say for example you wanted to call the Win32 MessageBeep method to output an audible prompt to your user when they type something incorrectly. Yes, I know that would be annoying but this is just an example.

First we need to call the Win32 assembly with P/Invoke like this.

[DllImport("User32.dll")]
static extern Boolean MessageBeep(UInt32 beepType);

Now this imports the MessageBeep functionality so we can use it in C#. Now all you do is call it like a normal Method passing the beep type as a parameter.

MessageBeep(0);

It is that simple. P/Invoke gives use some nice flexibility to leverage existing Win32 API functions that may be missing from C# or reuse unmanaged code we have written for other projects.

Popularity: 9% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.

Ruby Programmers Beware

ASP.Net, Ruby/Rails No Comments »

If you are a ruby programmer or a sys admin that maintains ruby programs on your network you should be aware, if you are not already, of some serious vulnerbilities that have been found in ruby.

Drew Yao of Apple has reported 5 exploits that allow an attacker to execute arbitrary code.

ruby-lang.org

Multiple vulnerabilities in Ruby may lead to a denial of service (DoS) condition or allow execution of arbitrary code.

The Ruby team have released patches for the 1.8 series (1.8.5 >) and the 1.9 series which should be implemented immediately.

The following issues are not likely to affect loyalty among the Ruby community, as expressed in the following comment on a blog covering this issue.

For the record… I love Ruby. I blame Drew!

Now the comment is obviously meant to be funny but most likely shows the true feelings of the community. They won’t let this deter them. They will just keep working to make Ruby better.

This may, however, have negative results on the preception of Enterprise level companies who, for the most part, haven’t embraced Ruby yet. Hopefuly this isn’t a setup back to furthering adoption outside the freelance community.

On another not Ruby may get a bit more exposure from an unlikely source. Microsoft is working on it’s implementation of Ruby, IronRuby, for the .Net CLR and making it compatible with their upcoming MVC framework. Could give Ruby some limelight in Enterprise companies that use .Net and give Ruby programmers a “foot in the door” so to speak.

Popularity: 14% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.

Friday Roundup for June 6, 2008

ASP.Net, Ruby/Rails No Comments »

Here is what I found interesting this week.

Exception handling best practices in ASP.NET web applications
Exception handling plays an important part of application management and user experience. If implemented correctly it can make maintenance easier and bring the user experience to a higher level. If not, it can be a disaster.

How many times have you seen the error message that doesn’t make any sense or at least provides some valuable information, or even better - how many times have you seen the famous error screen with exception message and a complete stack trace on yellow background? Too many times, I would say. This is why, among other things, some of my colleagues were very interested in exception handling techniques and best practices.

Did Rails Sink Twitter?
Twitter is arguably the most heavily used Ruby on Rails application in the world. Almost since its inception, Twitter has fostered a wildly passionate cult following. Also from the beginning, Twitter has suffered from chronic outages under that load.

In the past month, record downtime has prompted fresh outcry within its ever-growing user base. This, along with increased attention from mainstream media has forced Twitter to publicly acknowledge these issues, and to reveal a few details about the source of these problems.

Popularity: 16% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.

Friday Roundup for May 30, 2008

ASP.Net, News No Comments »

It seems I was a bit forgetful on Friday and forgot to post my roundup. Here is what I liked last week.

The future of .Net, Visual Studio, and more
As the information has been quite minimal, I decided to find out what kind of job postings Microsoft has listed at the moment as they reveal something of what they are planning at the Redmond. As it turns out, there are lot’s of interesting things coming in the future versions of .NET Framework, Visual Studio, SQL Server and SharePoint.

It should be noted that the information in this post is based mostly on combination of several job listings and speculations and it shouldn’t be taken as a definite road map or truth. Projects can be combined, canceled and delayed. I’ll link to the original job posting sources, but I’m not sure how long the links will work.

What is the future of C# anyways?
It was often asked during some of my presentations on F# and Functional C# about the future direction of C# and where I think it’s going. Last night I was pinged about this with my F# talk at the Philly ALT.NET meeting. The question was asked, why bother learning F#, when eventually I’ll get these things for free once they steal it and bring it to C#. Being the language geek that I am, I’m pretty interested in this question as well. Right now, the language itself keeps evolving at a rather quick pace as compared to C++ and Java. And we have many developers that are struggling to keep up with the evolution of the language to a more functional style with LINQ, lambda expressions, lazy evaluation, etc. There are plenty of places to go with the language and a few questions to ask along the way.

Something is wrong on the Internet
That comic sums up the internet in one sentence: the scrum of jostling opinions on the web and the optimism that truth can still win out. I was reminded of that comic when someone asked me about a particular way that someone recently tried to get links. Jonathan Crossfield wrote up a good background summary of the situation.

ASP.NET MVC Preview 3 Release
This morning we released the Preview 3 build of the ASP.NET MVC framework. I blogged details last month about an interim source release we did that included many of the changes with this Preview 3 release. Today’s build includes some additional features not in last month’s drop, some nice enhancements/refinements, as well as Visual Studio tool integration and documentation.

Popularity: 14% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.

Friday Roundup for May 23, 2008

ASP.Net, News No Comments »

Here is what I found interesting this week.

Entity Framework, Long term plans
I have since rewritten this slide to be more generic in “A single model cannot possibly be appropriate for all facets of your application including transactional behaviors, searching, and reporting”

Why use the Entity Framework? Yeah, why exactly?
I’ve spend the entire last 6 years of my life on something called Object-Relational Mapping, so I think I can comment on Danny’s claims a bit. Object-Relational Mapping, or O/R mapping or ‘ORM’ (for the people who aren’t aware of ORM being the acronym of Object Role Modelling) can be implemented in a lot of ways, and it is always used to solve a mismatch between two projections of an abstract entity model: the projection onto a relational schema and the projection onto an object oriented language. For more details, read my essay about this subject (don’t let the title feed you with presumptions about the contents). As this is rather abstract, let’s use an example: a very simple Order system

Google to become the hoax police?
Michael Gray has some thoughts on a quote by Matt Cutt’s and Google’s possible intent to censor what they deem as mis-leading information such as hoax stories. In my opinion, this is overstepping, in this case an understatement, their role as a search engine.

Popularity: 13% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.

Friday roundup for May2, 2008

ASP.Net, Fun, JavaScript/Ajax, Methodology No Comments »

Here is some highlights from this week.

Hijax
When I was originally writing the DOM Scripting book, its scope was very clear - it was to be an introductory work on JavaScript and the Document Object Model, with an emphasis on best practices. I made a conscious decision not to cover advanced topics like XMLHttpRequest.

But as the writing of the book progressed, Ajax really began to explode. It became clear that I’d have to at least mention the subject, even if I couldn’t cover it in detail. That’s where the book’s final chapter came from.

Jason Calacanis, Please Help Me Become A Pathetic Affiliate Marketer
I’m hoping Jason Calacanis can help me out here. Everyone knows that Jason loves to stir up trouble at Internet conferences and he recently caused quite a stir at Affiliate Summit West a couple months ago when he referred to Zac Johnson’s $300,000 check from Yahoo as pathetic.

Simple Subverison Repository Setup with VisualSVN Server
I have a few projects I am developing for my own company and choosing source control has really not needed much discussion. I use Subversion for everything because it’s free, works really well, has great community support and support a wide-variety of clients on many operating systems.

Why I’m a better software developer than you
What makes one developer better than another? Shouldn’t we all be performing at the same level? Of course not, we’re not sewing buttons on an assembly line. We’re using every bit of our intelligence to create something that we can only begin to understand.

Popularity: 17% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.

Friday roundup for April 25, 2008

ASP.Net, Browsers, JavaScript/Ajax, SQL, Silverlight, XHTML/CSS No Comments »

Here is what I liked this week. Enjoy!

Comparing Popular JavaScript/Ajax Frameworks
After four days of ASP.NET AJAX training with Stephen Walther I set out to learn more about my options in choosing a solution for a JavaScript/Ajax framework. If I realized days later I would be writing this comprehensive post on 7 of the most popular frameworks, I may have just went with the “Inny-Minny-Miney-Moe” method!

jQuery AJAX calls to a WCF REST Service
Since I’ve posted a few jQuery posts recently I’ve gotten a bunch of feedback to have more content on using jQuery in Ajax scenarios and showing some examples on how to use jQuery to cut out ASP.NET Ajax. In this post I’ll show how you can use jQuery to call a WCF REST service without requiring the ASP.NET AJAX ScriptManager and the client scripts that it loads by default. Note although I haven’t tried it recently the same approach should also work with ASMX style services.

SQL SERVER - Better Performance - LEFT JOIN or NOT IN?
First of all answer this question : Which method of T-SQL is better for performance LEFT JOIN or NOT IN when writing query? Answer is : It depends!

Video: Write Your First Silverlight Game
In this video, I demonstrate how to start writing your first Silverlight game. I show how to create a dramatic space scene, add a soundtrack, and associate movement with the mouse wheel. This is the first part of a two-part series.

Reading binary files using Ajax
But when it comes to binary files, helping hands from server-side technologies are often necessary.

So I googled around to see what I can do about binary files with Ajax and found this Marcus Granado’s post at http://mgran.blogspot.com/2006/08/downloading-binary-streams-with.html

What he posted there worked like a charm for FireFox and Safari but I couldn’t get it to work for IE.

But luckily, within the same page, someone had posted up a solution for IE as a comment, which is written in VBScript.

Safari CSS Masks
Webkit continues to impress with it’s early implementations of new standards. WebKit now supports alpha masks in CSS. Masks allow you to overlay the content of a box with a pattern that can be used to knock out portions of that box in the final display. In other words, you can clip to complex shapes based off the alpha of an image.

Popularity: 36% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.

ReadOnly and EnableViewState don’t play
nice

ASP.Net No Comments »

I ran into an interesting situation today. From what I read it an old issue as well. In ASP.Net 2.0 if you set EnableViewState equal to false and assign a text box as read only the value will be lost on post back.

Rick Strahl posted a nice article on this back in late 2005 which helped me get around this issue.

2005? What? Okay I have to admit that until today I have not been developing my web applications with view state turned completely off. I have turn individual controls on and off as performance required but view state has never been a real issue for me. Today I decided to bite the bullet and just get used to having view state off and it took less than an hour to run into this so here is how to get around it. Yeah I know you probably already know but on the off chance you don’t…then here it is.

Just place the following code in the Page_Load method.

TextBox1.Text = Request[TextBox1.UniqueID];

Popularity: 15% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.

Friday roundup for April 11, 2008

ASP.Net, News No Comments »

Here is what I liked this week.

Microsoft Sets Three Week Deadline for Yahoo! In Public Letter
In a letter sent today, Microsoft writes to Yahoo’s board of directors to tell them that they would like to ‘negotiate a definitive agreement on a combination of our companies.’ Their message is a combination of friend and foe: ‘If we have not concluded an agreement within the next three weeks, we will be compelled to take our case directly to your shareholders.’

How to Impress at Your Next Interview
I have had the opportunity to interview a lot of prospective developers. In that time I have come to understand more clearly what it is that impresses me most. And it might not be what you expect.

Google Takes Down HuddleChat After Complaints
The App Engine team was looking for some sample apps to help kick the tires on their new system, so we invited Googlers to build some as side projects. A couple of our colleagues here built HuddleChat in their spare time because they wanted to share work within their team more easily and thought persistent web chat would do the trick. We’ve heard some complaints from the developer community, though, so rather than divert attention from Google App Engine itself, we thought it better to just take HuddleChat down.”

Yahoo’s Big Day of Fun
It’s been a busy few days for Yahoo. With scorned love letters being passed back and forth with Microsoft, to adding video to Flickr, buying a Web analytics company and now testing out Google ads, you have to give Yahoo credit. We haven’t seen this much excitement coming out of the number two engine in quite some time.

How Much Is Our Blog Worth?
If we’d go by these rules, we wouldn’t sell our blog for anything less then: $344,236! Shocking amount isn’t it? And we haven’t even calculated the value of the brand you’d be selling. The “status” and numerous new networking opportunities that would come knocking when you buy an established blog.

The War Against Blog Commentors Who Use Keywords as Names
A couple months ago, I followed an interesting discussion at Remarkablogger related to a new comment policy that Michael Martine posted for his blog. It seems that Michael had become frustrated with the increasing number of comments on his blog that appeared to be more interested in plugging their websites keywords than adding to the dialogue. After some very interesting and thoughtful comments from his readers on the new policy change, Michael posted a slightly modified policy.

The empty try block mystery
I had used .NET Mass Downloader tool to download .NET 2.0 Framework sourcecode some time ago. While reading Timer.cs (System.Windows.Forms.Timer) and a couple of other classes I noticed something interesting.

Does the following piece of code look odd to you? Notice the empty “try” block and all processing being done in the “finally” block.

Popularity: 15% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.
WP Theme & Icons by N.Design Studio
Entries RSS Login