2010-05-27 34 views
0

我有正這樣的XML:如何使用LINQ綁定xml中的特定屬性?

<Profile> 
    <Interviewer id="111"> 
    <Questions> 
     <Question qid="1" Catid="1" CatagoryName="General"> 
     <Video url="http://10.1.8.45/BIGWeb/videofiles/user1/I1C1Vid1.mp4" /> 
     <Quest text="Tell me about yourself by intone ?" /> 
     <videofile text="I1C1Vid1.mp4" /> 
     </Question> 
     <Question qid="2" Catid="1" CatagoryName="General"> 
     <Video /> 
     <Quest text="What are your interestsone ?" /> 
     <videofile text="I1C1Vid2.mp4" /> 
     </Question> 
     <Question qid="3" Catid="1" CatagoryName="General"> 
     <Video /> 
     <Quest text="What is you hobbyone ?" /> 
     <videofile text="I1C1Vid3.mp4" /> 
     </Question> 

    </Questions> 
    </Interviewer> 
    <Interviewer id="222"> 
    <Questions> 
     <Question qid="1" Catid="1" CatagoryName="General"> 
     <Video url="http://10.1.8.45/BIGWeb/videofiles/user1/I1C1Vid1.mp4" /> 
     <Quest text="Tell me about yourself by intone ?" /> 
     <videofile text="I2C1Vid1" /> 
     </Question> 
     <Question qid="2" Catid="1" CatagoryName="General"> 
     <Video /> 
     <Quest text="What are your interestsone ?" /> 
     <videofile text="I2C1Vid2" /> 
     </Question> 
     <Question qid="3" Catid="1" CatagoryName="General"> 
     <Video /> 
     <Quest text="What is you hobbyone ?" /> 
     <videofile text="I2C1Vid3" /> 
     </Question> 
     <Question qid="4" Catid="1" CatagoryName="General"> 
     <Video /> 
     <Quest text="Your studiesone?" /> 
     <videofile text="I2C1Vid4" /> 
     </Question> 
     <Question qid="5" Catid="1" CatagoryName="General"> 
     <Video /> 
     <Quest text="What you want to be in the futureone ?" /> 
     <videofile text="I2C1Vid5" /> 
     </Question> 
     <Question qid="6" Catid="1" CatagoryName="General"> 
     <Video /> 
     <Quest text="What are your interests ?" /> 
     <videofile text="I2C1Vid6" /> 
     </Question> 
     <Question qid="7" Catid="1" CatagoryName="General"> 
     <Video /> 
     <Quest text="What is your hobby ?" /> 
     <videofile text="I2C1Vid7" /> 
     </Question> 
    </Questions> 
    </Interviewer> 
</profile> 

我想用url屬性和相應的錄像檔案和任務文本視頻。 注:我不希望在我的LINQ查詢視頻的空值......

這笏我做

var books = from Interviewer in xmlDocument.Descendants("Interviewer") 
      where Interviewer.Attribute("url").Value !=null 
     select new Data 
{ 
ques=(string)Interviewer.Element("Quest").Attribute("text").Value, 
videoid = (string)Interviewer.Element("videofile").Attribute("text").Value, 
videourl = (string)Interviewer.Element("Video").Attribute("url").Value 

}; 
return books.ToList(); 

它不工作請幫助... Thanx提前.... .................

+0

我認爲你需要再次複製和粘貼XML;另外請定義「不工作」,它在做什麼,它是一個錯誤,如果它不是一個錯誤你從linq查詢得到什麼結果。 – 2010-05-27 11:33:27

+0

海奔 絕對需要一個LINQ來獲取視頻網址和問題coressponding到該URL ......請救救我的天.... – senthila007 2010-05-27 11:38:55

回答

0

我不能告訴你想要在你的LINQ查詢中做什麼,你在select中的東西不會出現在結果中組。您行:

where Interviewer.Attribute("url").Value !=null 

會導致面試官只包含有一個URL屬性,凡在你的選擇你有

(string)Interviewer.Element("videofile").Attribute("text").Value 
(string)Interviewer.Element("Quest").Attribute("text").Value 

面試官將不包含錄像檔案和任務元素的元素,因爲他們沒有url屬性。唯一會在採訪者中成爲視頻元素的是他們是唯一具有url屬性的元素。

+0

檔案> <採訪人ID =「111」> <問題qid =「1」Catid =「1」CatagoryName =「General」>

+0

您的查詢將得到有URL屬性的所有元素,這將是視頻元素,因爲它們是隻有具有url屬性的元素。如果您選擇了「選擇Interviewer;」,那麼書籍將只包含視頻元素。然而,在你的選擇中,你試圖從你的where子句過濾掉的元素中提取數據,即沒有url屬性的元素。 – 2010-05-27 12:44:54

+0

hwo讓查詢看起來像 – senthila007 2010-05-27 13:03:35