簡體   English   中英

LINQ XML文檔

[英]LINQ XML Document

我有一個像這樣的簡單XML文檔

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

我正在嘗試獲取1和2的值。這就是我正在嘗試的。

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

問題在於這會返回1的值兩次,並返回1的值兩次,而不是1和2,im假定這是因為“ //”表示tompose匹配。 我想我的雙重問題是...

我該怎么做? 我將如何在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