简体   繁体   English

LINQ XML文档

[英]LINQ XML Document

I have a simple XML Document like this 我有一个像这样的简单XML文档

<Pictures>
  <Picture>
    <Source>1</Source>
    <Title>One</Title>
  </Picture>
  <Picture>
   <Source>2</Source>
   <Title>Two</Title>
  </Picture>
<Pictures>

Im trying to grab the values of 1 and 2. Heres what Im trying. 我正在尝试获取1和2的值。这就是我正在尝试的。

foreach (XmlNode mynode in doc.ChildNodes)
      {
        var source = mynode.SelectSingleNode("//Source").InnerText;
        var title = mynode.SelectSingleNode("//Title").InnerText;
      }

The problem is this returns the value of 1 twice, and the value of One twice, instead of 1 and 2, im assuming because the "//" means the topmose match. 问题在于这会返回1的值两次,并返回1的值两次,而不是1和2,im假定这是因为“ //”表示tompose匹配。 I guess my twofold question is... 我想我的双重问题是...

How would I do this? 我该怎么做? How would I do this in Linq? 我将如何在Linq中做到这一点?

var xDoc = XDocument.Load("path");

var pictures = xDoc.Root
           .Elements("Picture")
           .Select(x => new 
                        {  
                            source = (string)x.Element("Source"),
                            title = (string)x.Element("Title")
                        }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM