假设有两个如下所示的XML文件:
<Customers>
<Customer CustomerID="alc">Alice</Customer>
</Customer>
并且
<Orders>
<Order OrderID="001" CID="alc">apple</Order>
</Orders>
现在我需要将这两个 XML 文件合并,并使用内连接创建一个新的文件:
<Result>
<Join>
<Customer CustomerID="alc">Alice</Customer>
<Order OrderID="001" CID="alc">apple</Order>
</Join>
</Result>
我有一个查询:
var result = new XElement("Result",
from customer in XElement.Load("Customers.xml").Elements("Customer")
join order in XElement.Load("Orders.xml").Elements("Order")
on
(string)customer.Attribute("CustomerID")
equals
(string)order.Attribute("CID")
select new XElement("Join",
new XElement("Customer", (string)customer.Element("Customer"),
new XAttribute("CustomerID", (string)customer.Attribute("CustomerID"))),
new XElement("Order", (string)order.Element("Order"),
new XAttribute("OrderID", (string)order.Attribute("OrderID")),
new XAttribute("CID", (string)order.Attribute("CID")))));
result.Save("result.xml");
用这个方法,我无法获取到数据"Alice"和"apple"。结果如下:
<Result>
<Join>
<Customer CustomerID="alc" />
<Order OrderID="001" CID="alc" />
</Join>
</Result>
我认为(string)customer.Element("Customer")
和(string)order.Element("Order")
可能存在问题,但我不知道如何修改。