如何在Linq中使用xpath来检查元素和属性

时间:2015-08-09 08:25:08

标签: c# xml linq join xpath

我试图加入两个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>

文件二,此处的连接键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>

我当前的加入操作基于此问题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")
                        );

3 个答案:

答案 0 :(得分:2)

您可以表达在连接条件中检索元素或属性的逻辑:

var bookInfos =
    from a in fileone.Descendants("book")
    let aBookID = (string)a.Element("bookID") ?? (string)a.Attribute("bookID")
    join b in filetwo.Descendants("book")
        on aBookID equals (string)b.Element("bookID") ?? (string)b.Attribute("bookID")
    select new XElement("bookInfo",
        aBookID,
        a.Element("name"),
        b.Element("content")
    );

答案 1 :(得分:2)

您可以使用null coalescing operator ??首先检查是否存在名为"bookID"的属性,如果不存在,则检查该名称的元素:

        var bookInfos =
              from a in fileone.Descendants("book")
              join b in filetwo.Descendants("book")
                  on ((string)a.Attribute("bookID") ?? (string)a.Element("bookID")) equals ((string)b.Attribute("bookID") ?? (string)b.Element("bookID"))
              select new XElement("bookInfo",
                                      new XElement("bookID", (string)a.Attribute("bookID") ?? (string)a.Element("bookID")),
                                      a.Element("name"),
                                      b.Element("content")
                                  );

答案 2 :(得分:0)

试试这个

on (string)a.Element("bookID") equals (string)(b.Attribute("bookID")) 

请参阅fiddle