使用linq查找内部节点值的最佳方法

本文关键字:最佳 方法 内部节点 linq 查找 使用 | 更新日期: 2023-09-27 18:18:50

看起来像下面提供的示例。我需要找到ImagesImage节点

下的所有值
<root>
  <Title>Press Relese 1</Title>
  <ReleaseDate>2014-07-24</ReleaseDate>
  <Description>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </Description>
  <Template>2</Template>
  <Images>
    <Image>
      <img src="/uploadedImages/MainSite/Content/PressReleases/300x340-slide01.png?n=3011" alt="slide01" title="slide01" />
    </Image>
  </Images>
  <Images>
    <Image>
      <img src="/uploadedImages/MainSite/Content/PressReleases/300x340-slide02.jpg?n=8663" alt="slide02" title="slide02" />
    </Image>
  </Images>
  <Images>
    <Image>
      <img src="/uploadedImages/MainSite/Content/PressReleases/300x340-slide02.jpg?n=6982" alt="slide02" title="slide02" />
    </Image>
  </Images>
  <Images>
    <Image>
      <img src="/uploadedImages/MainSite/Content/PressReleases/300x340-slide04.jpg?n=9958" alt="slide04" />
    </Image>
  </Images>
</root>
谁能建议使用linq的最佳解决方案?

这是我尝试过的代码,但我没有得到图像列表,我有什么问题吗?

var pressContentDetail = (from bdata in xmlDoc.Descendants("root")
                              select new
                              {
                                  Title = (string)bdata.Element("Title"),
                                  Description = (string)bdata.Element("Description"),
                                  Template = (string)bdata.Element("Template"),
                                  SlideImages = bdata.Descendants("Images")
                              }).ToList();

使用linq查找内部节点值的最佳方法

try this

var pressContentDetail = (from bdata in xmlDoc.Descendants("root")
                              select new
                              {
                                  Title = (string)bdata.Element("Title"),
                                  Description = (string)bdata.Element("Description"),
                                  Template = (string)bdata.Element("Template"),
                                  SlideImages = bdata.Descendants("Image").Elements().Select(x=>x.ToString())
                              }).ToList();  

SlideImages的值:

<img src="/uploadedImages/MainSite/Content/PressReleases/300x340-slide01.png?n=3011" alt="slide01" title="slide01" />
<img src="/uploadedImages/MainSite/Content/PressReleases/300x340-slide02.jpg?n=8663" alt="slide02" title="slide02" />
<img src="/uploadedImages/MainSite/Content/PressReleases/300x340-slide02.jpg?n=6982" alt="slide02" title="slide02" />
<img src="/uploadedImages/MainSite/Content/PressReleases/300x340-slide04.jpg?n=9958" alt="slide04" />

这是一个有点奇怪,你在多个<Images>元素有一个单一的<Image>元素,其中包含一个<img>元素,但前提是这是正确的XML,你可以提取图像的src属性使用此代码:

var xDocument = XDocument.Parse(xml);
var pressContentDetail = new {
  Title = (String) xDocument.Root.Element("Title"),
  Description = (String) xDocument.Root.Element("Description"),
  Template = (Int32) xDocument.Root.Element("Template"),
  SlideImages = xDocument
    .Root
    .Elements("Images")
    .Select(x => (String) x.Element("Image").Element("img").Attribute("src"))
    .ToList()
};

获取<Images>元素的集合,我使用xDocument.Root.Elements("Images")。对于每一个元素,我使用Select通过导航到XML层次结构(String) x.Element("Image").Element("img").Attribute("src")来投影src属性的值。

将SlideImages作为字符串类型。然后将SlideImages数据绑定到Image控件。试试这个。我希望这能奏效。

var pressContentDetail = (from bdata in xmlDoc.Descendants("root")
                          select new
                          {
                              Title = (string)bdata.Element("Title"),
                              Description = (string)bdata.Element("Description"),
                              Template = (string)bdata.Element("Template"),
                              SlideImages = (string) bdata.Descendants("Images")
                          }).ToList();