2012-02-13 77 views
0

我有被佈置的就像XML訪問包含

<AllReport> 
    <Report> 
      <DataPoint1> 
      </DataPoint1> 
      <DataPoint2> 
      </DataPoint2> 
      <DataPoint3> 
      </DataPoint3>  
    </Report> 
    <Report> 
      <DataPoint1> 
      </DataPoint1> 
      <DataPoint2> 
      </DataPoint2> 
      <DataPoint3> 
      </DataPoint3> 
    </Report> 
    so on so forth.... 
</AllReport> 

一個XML結構文件有沒有更好的辦法直接訪問DataPoint1不使用多脂肪的循環?

+1

在哪種編程語言? – Tomalak 2012-02-13 09:51:41

+0

比什麼更好? – 2012-02-13 10:07:02

回答

0

在C#中,你可以使用LINQ到XML

var xmlItems = XDocument.Load("yourXML); 
var result = from e in xmlItems.Descendants("Report").Descendants("DataPoint1"); 

foreach(item in result){ 

    //it goes though all DataPoint 1 

} 
0

的XPath表達式/AllReport/Report/DataPoint1//DataPoint1都將你的樣本文檔中返回所有DataPoint1節點的列表。然後,您可以使用DOM庫或所選語言爲您提供的任何方式遍歷該列表。

您使用哪一個將取決於實際文檔的性質。如果您希望訪問的每個DataPoint1實例都以相同的方式嵌套,請使用前者。如果你不能確定的DataPoint1嵌套(也許有些報道有子報表,或有一個「摘要」 DataPoint1孩子AllReport,那麼後者可能是可取的。

它通常是最好的選擇更精確的表達,出於性能和錯誤檢測原因。