2014-07-21 48 views
1

希望有人可以用這個指向正確的方向。迭代通過數據集獲取子行

我想通過一個XML文件進行迭代,並得到在XML文件中的每個「表」,並得到各子行等

我能得到不屬於子行等就好行。

這裏是xml結構的一個例子。

<items> 
    <item> 
    <id>1</id> 
    <row1>abc</row1> 
    <row2>abc</row2> 
    <needthis> 
     <first>John</first> 
     <last>John</last> 
    </needthis> 
    </item> 
</items> 

這就是我目前使用的。

foreach (DataTable table in ds.Tables) 
{ 
    foreach (DataRow row in table.Rows) 
    { 
    foreach (DataColumn column in table.Columns) 
    { 
     object item = row["id"]; 
     object item2 = row["row1"]; 
     //Just to see what is returning 
     System.Windows.Forms.MessageBox.Show(item.ToString()); 
     System.Windows.Forms.MessageBox.Show(item2.ToString()); 
     } 
    } 
    } 

我需要什麼來獲得needthis表的第一行和最後一行?

+0

你試過[DataRow.GetChildRows](http://msdn.microsoft.com/en-us/library/system.data.datarow.getchildrows(v = vs.110).aspx)? –

+0

您是否有使用數據集的原因,而不僅僅是解析XML?這似乎是一個更容易維護的方法。 –

+0

@MatthewHaugen我是一切爲了更有意義和更容易。這是我第一次必須使用C#讀取/解析和xml文件,而且是我遇到的第一個例子,似乎工作,直到我需要子行等。 – Bowenac

回答

2

我個人喜歡Linq XML這種東西。

//Probably want to use Load if you're using a file 
XDocument xDoc = 
     XDocument.Parse (@" 
     <items> 
      <item> 
      <id>1</id> 
      <row1>abc</row1> 
      <row2>abc</row2> 
      <needthis> 
      <first>John</first> 
      <last>John</last> 
      </needthis> 
      </item> 
     </items>"); 

var items = from item in xDoc.Descendants("item") 
      from needThis in item.Descendants("needthis") 
      select new 
      {  Id = item.Element("id").Value, 
        Row1 = item.Element("row1").Value, 
        first = needThis.Element("first").Value, 
        last = needThis.Element("last").Value 
      }; 

foreach (var item in items) 
{ 
    Console.WriteLine(item.Id); 
    Console.WriteLine(item.Row1); 
    Console.WriteLine(item.first); 
    Console.WriteLine(item.last); 
} 

如果由於某種原因,你真的需要使用的數據集,你需要使用的DataRelation做以下

  1. 沒有環比數據表集合,但只使用項目表
  2. 使用GetChildRows 「item_needthis」您可以通過檢查DataSet找到數據關係名稱.Relations集合


using(MemoryStream stream = new MemoryStream()) 
{ 
     StreamWriter writer = new StreamWriter(stream); 
     writer.Write(@" 
      <items> 
       <item> 
       <id>1</id> 
       <row1>abc</row1> 
       <row2>abc</row2> 
       <needthis> 
       <first>John</first> 
       <last>John</last> 
       </needthis> 
       </item> 
      </items>"); 
     writer.Flush(); 
     stream.Position = 0; 

    DataSet ds = new DataSet(); 
    ds.ReadXml(stream); 


    DataTable table = ds.Tables["item"]; 

    foreach (DataRow row in table.Rows) 
    { 
     Console.WriteLine(row["id"]); 
     Console.WriteLine(row["row1"]); 
     var ChildRows = row.GetChildRows("item_needthis"); 
     foreach(var ntRow in ChildRows) 
     { 
      Console.WriteLine(ntRow["first"]); 
      Console.WriteLine(ntRow["last"]); 
     } 
    } 


} 

你也可以使用LINQ到數據集

+0

完美的謝謝。 – Bowenac

+0

@Bowenac沒問題。我還爲您提到的問題添加了一個答案,以防其他人遇到此問題並需要使用數據集 –

0

我會用XElement類,而不是數據集。然後,你可以即使只有這個閱讀您的文件:

XElement root = XElement.Load(...); // or Parse(...) 

return root.Elements("item").Select(c => new { id = c.Element("id").Value, 
               row1 = c.Element("row1").Value, 
               row2 = c.Element("row2").Value, 
               needthis = new { first = c.Element("needthis").Element("first").Value, 
                   last = c.Element("needthis").Element("last").Value } }); 

當然,我沒有測試過這一點,但你可以看到這一點。它也沒有任何錯誤處理,並且效率更高,但是你可以看到它的工作原理。