【问题标题】:How to bind a specific attribute from an xml using LINQ?如何使用 LINQ 绑定来自 xml 的特定属性?
【发布时间】:2010-05-27 20:02:05
【问题描述】:

我有一个这样的 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 查询中使用 null 视频值......

这是我做的

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 HELP...thanx in ADVANCE .....

【问题讨论】:

  • 我认为你需要再次复制和粘贴 XML ;-) 另外请定义“不工作”,它在做什么,是不是错误,如果不是错误你是什么结果从 linq 查询中获取。
  • hai ben jus 需要一个 linq 来获取视频 url 和对应于该 url 的问题......请节省我的时间......

标签: linq-to-xml


【解决方案1】:

我不知道您在 LINQ 查询中尝试做什么,您在选择中的内容不会出现在结果集中。您的线路:

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 属性的元素。

【讨论】:

  • 简介> 如何仅使用 url 属性获取视频元素
  • 您查询将获得所有具有 url 属性的元素,这将是视频元素,因为它们是唯一具有 url 属性的元素。如果您的选择说“选择采访者;”,那么书籍将只包含视频元素。但是,在您的选择中,您试图从 where 子句过滤掉的元素中提取数据,即没有 url 属性的元素。
猜你喜欢
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多