我有存储在XDocuments和数据表的数据的集合,我想要解决这两个与XPath查询一个统一的数据空间。因此,例如,/根/表/订单/姓会取名字列的值的数据表名为订单每排。
I have a collection of data stored in XDocuments and DataTables, and I'd like to address both as a single unified data space with XPath queries. So, for example, "/Root/Tables/Orders/FirstName" would fetch the value of the Firstname column in every row of the DataTable named "Orders".
有没有办法做到这一点不复制所有记录在数据表中插入的XDocument?
Is there a way to do this without copying all of the records in the DataTable into the XDocument?
我使用.net 3.5
I'm using .Net 3.5
我终于想通了这个问题的答案我自己。我发现了一类在System.Xml.Linq的称为XStreamingElement,可以从一个LINQ前pression上创建即时XML结构。下面是铸造一个DataTable成XML空间的一个例子。
I eventually figured out the answer to this myself. I discovered a class in System.Xml.LINQ called XStreamingElement that can create an XML structure on-the-fly from a LINQ expression. Here's an example of casting a DataTable into an XML-space.
Dictionary<string,DataTable> Tables = new Dictionary<string,DataTable>();
// ... populate dictionary of tables ...
XElement TableRoot = new XStreamingElement("Tables",
from t in Tables
select new XStreamingElement(t.Key,
from DataRow r in t.Value.Rows
select new XStreamingElement("row",
from DataColumn c in t.Value.Columns
select new XElement(c.ColumnName, r[c])))))
其结果是一个的XElement(TableRoot)类似以下内容的结构,假定字典包含一个表称为订单有两排。
The result is an XElement (TableRoot) with a structure similar to the following, assuming the dictionary contains one table called "Orders" with two rows.
<Tables>
<Orders>
<row>
<sku>12345</sku>
<quantity>2</quantity>
<price>5.95</price>
</row>
<row>
<sku>54321</sku>
<quantity>3</quantity>
<price>2.95</price>
</row>
</Orders>
</Tables>
这可以合并具有较大的XElement /的XDocument基于层次和查询使用XPath。
That can be merged with a larger XElement/XDocument based hierarchy and queried with XPath.