C# 根据C中的元素值比较两个xml文档中的元素

C# 根据C中的元素值比较两个xml文档中的元素,c#,xml,linq,join,element,C#,Xml,Linq,Join,Element,我试图基于相同的元素连接两个XML,但我的代码没有返回任何内容。var结果为空。有人能帮忙解决这个问题吗?非常感谢 文件一: <bookstore> <book> <bookID>100</bookID> <name> The cat in the hat </name> </book> <book> <bookID>90</bookID

我试图基于相同的元素连接两个XML,但我的代码没有返回任何内容。var结果为空。有人能帮忙解决这个问题吗?非常感谢

文件一:

<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、name和content元素的元素:

var bookInfos =
        from a in fileone.Descendants("book")
        join b in filetwo.Descendants("book")
            on (string)a.Element("bookID") equals (string)b.Element("bookID")
        select new XElement("bookInfo", 
                                a.Element("bookID"), 
                                a.Element("name"), 
                                b.Element("content")
                            );
var result = new XElement("result", bookInfos);
Console.WriteLine(result.ToString());
输出:


我还尝试比较bookstore.ElementbookID,但它也返回一个空文件。谢谢您的回答!但是,如果我想在fileone中保留其余的项值,而不指定元素内容,我该怎么办?我的意思是在FileOne中可能还有其他元素,比如…等等,你可以说:选择新的XElementbookInfo,a.元素名称,b.元素。这将只从第一个XML中获取name元素,从第二个XML中获取bookID、content等所有元素
<result>
    <bookInfo>
       <bookID>103</bookID>
       <name> a new book </name>
       <content> bio </content>
    <bookInfo>
 </result>
var reslut =    
                from a in fileone.Descendants("bookstore")
                join b in filetwo.Descendants("bookstore")

            on (string)fileone.Descendants("bookID").First() equals (string)filetwo.Descendants(""bookID"").First() 
            select new XElement("bookInfo", a, b);
var bookInfos =
        from a in fileone.Descendants("book")
        join b in filetwo.Descendants("book")
            on (string)a.Element("bookID") equals (string)b.Element("bookID")
        select new XElement("bookInfo", 
                                a.Element("bookID"), 
                                a.Element("name"), 
                                b.Element("content")
                            );
var result = new XElement("result", bookInfos);
Console.WriteLine(result.ToString());
<result>
  <bookInfo>
    <bookID>100</bookID>
    <name> The cat in the hat </name>
    <content> story </content>
  </bookInfo>
  <bookInfo>
    <bookID>90</bookID>
    <name> another book </name>
    <content> fiction </content>
  </bookInfo>
  <bookInfo>
    <bookID>103</bookID>
    <name> a new book </name>
    <content> bio </content>
  </bookInfo>
</result>