【发布时间】:2015-08-09 08:25:07
【问题描述】:
我正在尝试连接两个 XML 文件,连接键有时作为元素出现,但有时作为属性出现,如下面的 XML 文件所示。如何使用 XPath 或 ||运营商解决问题?任何其他解决方案也非常感谢。提前谢谢了!
文件一:
<bookstore>
<book>
<bookID>100</bookID>
<name> The cat in the hat </name>
</book>
<book>
<bookID>90</bookID>
<name> another book </name>
</book>
<book>
<bookID>103</bookID>
<name> a new book </name>
</book>
</bookstore>
文件二,这里的join key bookID是属性:
<bookstore>
<book bookID=100>
<content> story </content>
</book>
<book bookID=90>
<content> fiction </content>
</book>
<book bookID=103>
<content> bio </content>
</book>
我想要的结果是
<result>
<bookInfo>
<bookID>103</bookID>
<name> a new book </name>
<content> bio </content>
<bookInfo>
</result>
我目前基于这个问题的join操作Compare elements from two xml documents based on element value in C#
var bookInfos =
from a in fileone.Descendants("book")
join b in filetwo.Descendants("book")
on (string)a.Element("bookID") equals (string)b.Element("bookID") //how can I change the Join condition as the key might attributes in any of the two files?
select new XElement("bookInfo",
a.Element("bookID"),
a.Element("name"),
b.Element("content")
);
【问题讨论】: