stand on the shoulders of giants

[Tips and Tricks] LINQ to XML with xmlns

If your XML file has xmlns, you may be run into problem when get some XML data with LINQ.

How to fix this?

1. prepend the namespace

XNamespace ns = doc.Root.Name.Namespace;
var cars2 = from d in doc.Descendants(ns + "CarForSale") select d;

2. search by local name:

var cars2 = from d in doc.Descendants() 
            where d.Name.LocalName == "CarForSale" 
            select d;

posted @ 2010-06-29 16:36  DylanWind  阅读(303)  评论(0编辑  收藏  举报