LINQ To XML : Descendants函式

LINQ To XML : Descendants函式
 

    在LINQ To XML 的架構中,若想取得某個Element下的指定之子Element,可以呼叫Descendants這個Extension Method,如下所示:
foreach (XElement elem in doc.Elements("Customers").Descendants("Customer"))
      Console.WriteLine(string.Format("Customer ID : {0}, Name : {1}, Address : {2}",
                                                elem.Attribute("ID").Value,
                                                elem.Attribute("Name").Value,
                                                elem.Attribute("Address").Value));

此例所使用的XML如下:
<?xml version="1.0" encoding="utf-8"?>

 
 
 

不過,這有個特別的情況,當XML中包含了Namespace時,傳入Descendants的指定Element名稱就必須包含Namespace,舉個例來說,下面的.dbml(LINQ To SQL的定義檔)。
<?xmlversion ="1.0"encoding="utf-8"?>
        xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
 "Data Source=JEFFRAY;Initial Catalog=Northwind;Integrated Security=True"SettingsObjectName="LTSync.Properties.Settings"SettingsPropertyName="NorthwindConnectionString"Provider="System.Data.SqlClient" />
...........
 

如果用下面的程式來列舉Connection Element,會以失敗收場。
var cstr = (from s1 in doc.Descendants("Connection") select s1).First();

問題的徵結點在於.dbml中定義了Namespace,而我們於呼叫Descendants時,只傳入Element的LocalName部份所致,仔細查看Descendants函式的宣告,你會發現其事實上接收的是一個XName型別的物件,由於XName實作了隱含轉型運算子,所以很自然的將我們傳入的字串隱含轉型為XName,只是此
時得到的XName物件中只包含了LocalName部份,並未包含Namespace部份。
要順利取得Connection Element,我們可以用下面的程式碼來達成。
var cstr = (from s1 in doc.Descendants() where s1.Name.LocalName == "Connection"select s1).First(); 

此例中我們以未帶參數的Descendants函式來取得所有子Element,並一一比對其LocalName,忽略掉Namespace。
另一個手法是直接以Root Element的Namespace來疊加LocalName,如下所示:
var cstr = (from s1 in doc.Descendants(doc.Root.Name.Namespace+"Connection") select s1).First(); 

此手法的缺點是,欲取得的Element之Namespace必須與Root Element相同。

详细出处参考:http://www.itqun.net/content-detail/140097_2.html

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16436858/viewspace-630025/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/16436858/viewspace-630025/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值