簡體   English   中英

遍歷數據集獲取子行

[英]Iterate through dataset get child rows

希望有人能指出我正確的方向。

我試圖遍歷一個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());
      } 
    }
  }

我需要什么來獲得這個表的第一行和最后一行?

就我個人而言,我喜歡Linq to 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);
}

如果由於某種原因您確實需要使用數據集,則需要執行以下操作

  1. 不要遍歷DataTable集合,只需使用項目表
  2. 使用DataRelation“item_needthis”使用GetChildRows您可以通過檢查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到數據集

我會使用XElement類而不是DataSet。 然后你就可以讀到你的文件,即使這樣:

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 } });

當然我沒有測試過這個,但你可以看到這一點。 它也沒有任何錯誤處理,它可能更有效,但你再次可以看到它的工作原理的要點。

暫無
暫無

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

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