2012-02-16 64 views
1

我有一個XML文件作爲下使用XDocument解析數據不會產生任何結果 - 爲什麼?

<!--Sample Person Data--> 
<Person> 
    <PersonDetail PersonId="1"> 
    <Name>Priyanka Das</Name> 
    <Age>30</Age> 
    <Sex>Male</Sex> 
    <LivesIn>Bangalore</LivesIn> 
    <Email>[email protected]</Email> 
    </PersonDetail> 
    <PersonDetail PersonId="2"> 
    <Name>Shashidhar Nikatani</Name> 
    <Age>40</Age> 
    <Sex>Male</Sex> 
    <LivesIn>Bangalore</LivesIn> 
    <Email>[email protected]</Email> 
    </PersonDetail> 
    <PersonDetail PersonId="3"> 
    <Name>Arundhuti Roy</Name> 
    <Age>27</Age> 
    <Sex>Female</Sex> 
    <LivesIn>Kerala</LivesIn> 
    <Email>[email protected]</Email> 
    </PersonDetail> 
    <PersonDetail PersonId="4"> 
    <Name>Nitin Mallik</Name> 
    <Age>28</Age> 
    <Sex>Male</Sex> 
    <LivesIn>Bangalore</LivesIn> 
    <Email>[email protected]</Email> 
    </PersonDetail> 
    <PersonDetail PersonId="5"> 
    <Name>Essaki Raja Kandaswamy</Name> 
    <Age>27</Age> 
    <Sex>Male</Sex> 
    <LivesIn>Madras</LivesIn> 
    <Email>[email protected]</Email> 
    </PersonDetail> 
</Person> 

我需要找出人誰住在班加羅爾的細節。

我也做了以下,但沒能獲得任何結果

XDocument xmlSource = null; 
    xmlSource = XDocument.Load(@"D:\Personsample.xml"); 

      var query = from data in xmlSource.Descendants("Person") 
         where (string)data.Element("LivesIn") == "Bangalore" 
         select data; 

幫助需要

+2

它不應該是'從數據xmlSource.Descendants( 「PersonDetail」)'? – 2012-02-16 02:47:04

回答

3

有「人」的直接「LivesIn」孩子,所以data.Element("LivesIn")總是會不會產生任何東西。

你確定你不是這個意思:

from data in xmlSource.Descendants("PersonDetail") 
        where (string)data.Element("LivesIn") == "Bangalore" 
        select data; 
相關問題