繁体   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