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

时间:2014-08-20 06:13:59

标签: c# linq linq-to-xml

我看起来像下面提供的样本。 我需要找到图像图像节点

下的所有值
<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();

3 个答案:

答案 0 :(得分:2)

试试这个

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

在多个<Images>元素内部包含<Image>元素的单个<img>元素,但是如果这是正确的XML,您可以提取{{{}>有点奇怪1}}使用此代码的图像属性:

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")通过向下导航XML层次结构Select来投影src属性的值。