2010-05-27 35 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