English 中文(简体)
将两个 XML 文件与 IE 数数比较。 除 () 外
原标题:Comparing two XML files with IEnumerable.Except()

代码如下:

    XDocument aDoc = XDocument.Load(fileA);
    XDocument bDoc = XDocument.Load(fileB);

    var commonfromA = aDoc.Descendants("Project").Except(bDoc.Descendants("Project")); 

我比较了以下XML:

a Doc.xml 医生

<Employees>
      <Project ID="1" Name="Project1"/>
      <Project ID="2" Name="Project2"/>
</Employees>

b. xml 医生

<Employees>
  <Project ID="1" Name="Project1"/>
  <Project ID="3" Name="Project3"/>
</Employees> 

当我执行我得到的密码

<Project ID="1" Name="Project1"/>
<Project ID="2" Name="Project2"/>

而不是

<Project ID="2" Name="Project2"/> **Which is the elements that are in A but not in B**

提前感谢您

最佳回答

是的, 因为文件“ 它们是/ 它们” 中没有任何元素实际上在 bDoc 中。 如果您为父母询问其中的每一个元素, 它们将会报告不同的结果 。

如果你在刚拿到身份证明时高兴的话 很简单

var idsJustInA = aDoc.Descendants("Project")
                     .Select(x => (int) x.Attribute("ID"))
                     .Except(bDoc.Descendants("Project"))
                                 .Select(x => (int) x.Attribute("ID")));

如果您想要元素本身, 您可以 < / em> 通过 < code> > Iequal Comptarer< XElement> 来用 ID 比较元素, 或者您可以使用类似 < a href=" http://code.google. com/ p/ morelinq/ source/browse/ MoreLinq/ExeptBy. cs" rel=" nofolp" {code> expendBy 来使用 < a href=" "http://morelinq.goglecode.com" rel=" nofolpol" > MoreLIN_ / a > :

var justInA = aDoc.Descendants("Project")
                  .ExceptBy(bDoc.Descendants("Project"),
                            x => (int) x.Attribute("ID"));
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签