简体   繁体   English

遍历数据集获取子行

[英]Iterate through dataset get child rows

Hoping someone can point me in the right direction with this. 希望有人能指出我正确的方向。

I am trying to iterate through an xml file and get each "table" in the xml file, and get each child rows etc. 我试图遍历一个xml文件并获取xml文件中的每个“表”,并获取每个子行等。

I am able to get the rows that are not child rows etc just fine. 我能够得到不是子行等的行就好了。

Here is an example of the xml structure. 这是一个xml结构的例子。

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

This is what I am currently using. 这就是我目前正在使用的。

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

What would I need to get the first row and last row of needthis table? 我需要什么来获得这个表的第一行和最后一行?

Personally I like Linq to XML for this kind of thing. 就我个人而言,我喜欢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);
}

If for some reason you really needed to use datasets you will need to do the following 如果由于某种原因您确实需要使用数据集,则需要执行以下操作

  1. Not loop over the DataTable collection but just use the item Table 不要遍历DataTable集合,只需使用项目表
  2. Use GetChildRows using the DataRelation "item_needthis" You can find the data relation name by inspecting the DataSet.Relations Collection 使用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"]);
        }
   }


}

You could also use Linq to Datasets 您也可以使用Linq到数据集

I would use the XElement class instead of DataSets. 我会使用XElement类而不是DataSet。 Then you could read your file even with just this: 然后你就可以读到你的文件,即使这样:

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

Of course I haven't tested this, but you can see the point. 当然我没有测试过这个,但你可以看到这一点。 It also doesn't have any error handling, and it could be more efficient, but again you can see the gist of how it works. 它也没有任何错误处理,它可能更有效,但你再次可以看到它的工作原理的要点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM