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

发布于 2024-09-03 16:03:25 字数 3095 浏览 2 评论 0原文

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

不起作用,请帮助……谢谢 ADVANCE …… ……

i have a n xml like this:

<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>

i want video with url attribute and corresponding videofile and quest text.
Note:i dont want null values of video in my LINQ query......

this wat i did

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();

its not working PLS HELP... thanx in ADVANCE.....................

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2024-09-10 16:03:25

我不知道你想在 LINQ 查询中做什么,你的选择中的内容不会出现在结果集中。您的行:

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

将导致 Interviewer 仅包含具有 url 属性的元素,而在您的选择中,

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

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

I can't tell what you are trying to do in your LINQ query, your have things in the select that will not be in the result set. Your line:

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

will cause interviewer to only contain elements that have a url attribute, where in your select you have

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

Interviewer will not contain videofile and quest elements as they do not have a url attribute. The only thing that will be in interviewer will be your video elements as they are the only elements that have a url attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文