簡體   English   中英

無法在C#中使用XElement遍歷src標記

[英]Unable to iterate over the src tags using XElement in C#

我有以下結構的XML。

<DataElements>
  <source> <...Information goes here...></source>
    <destination> <......information goes here....</destination>
    <destination> <......information goes here....</destination>
    <destination> <......information goes here....</destination>
<DataElements>

我正在使用foreach遍歷XML中的每個子元素。 我能夠遍歷標記,而無法理解如何遍歷所有 目標標記。

我在下面放置了一段代碼。 請指導我如何解決此問題。

foreach (XElement element in doc.Root.Elements(Elements.DataElements))
{
    //code goes here
  foreach (XElement subelement in    
  element.Elements(Elements.Destination))  --> i seem to iterate only on the first destination tag rather than all of them
  {

  }

} 

Destination引用字符串“ destination”,DataElement引用字符串“ DataElements”。

根據您提供的內容,您只需要執行以下操作:

foreach (XElement destinationElement in doc.Root.Elements("destination"))
{
     // This will loop through all child elements named 'destination'
}

我假設Elements.Destination ==“ destination”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM