我想有两个 datatables
叫做 bndbox
来显示2组坐标,如上面的代码所示。但我得到了一个错误,因为 bndbox
已经存在。然后我创建了 object
以期能抱 bndbox
与 object1
和 object2
但这并不奏效。有什么办法可以让两个 datatables
同名 dataset
? 还是不可能?理想的输出是这样的。
<annotation>
<folder>Kangaroo</folder>
<filename>00151.jpg</filename>
<size>
<width>503</width>
<height>488</height>
<depth>3</depth>
</size>
<object>
<bndbox>
<ID>1</ID>
<xmin>209</xmin>
<ymin>186</ymin>
<xmax>294</xmax>
<ymax>217</ymax>
</bndbox>
</object>
<object>
<bndbox>
<ID>1</ID>
<xmin>301</xmin>
<ymin>163</ymin>
<xmax>348</xmax>
<ymax>178</ymax>
</bndbox>
</object>
</annotation>
你只需要一个多行的bndbox。 试试下面的Xml Linq代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
DataTable dt = new DataTable("BndBox");
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("xmin", typeof(int));
dt.Columns.Add("ymin", typeof(int));
dt.Columns.Add("xmax", typeof(int));
dt.Columns.Add("ymax", typeof(int));
foreach (XElement bndbox in doc.Descendants("bndbox"))
{
dt.Rows.Add(new object[] {
(int)bndbox.Element("ID"),
(int)bndbox.Element("xmin"),
(int)bndbox.Element("ymin"),
(int)bndbox.Element("xmax"),
(int)bndbox.Element("ymax")
});
}
}
}
}