如何使用LINQ绑定xml中的特定属性?

时间:2010-05-27 11:30:38

标签: linq-to-xml

我有一个像这样的n 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();

它没有工作的PLS帮助...在ADVANCE .....................

1 个答案:

答案 0 :(得分:0)

我不知道你在LINQ查询中想要做什么,你在select中的东西不会出现在结果集中。你的行:

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

会导致访问者只包含具有url属性的元素,在你的选择中你有

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

Interviewer不包含videofile和quest元素,因为它们没有url属性。面试官中唯一的东西就是你的视频元素,因为它们是唯一具有url属性的元素。