使用LINQ查询XML以返回不同的子元素

最小德鲁伊

考虑以下两个XML文档。

原来的

<Stock>
   <Vehicle id="123456">
      <Name>Ford Ka</Name>
      <Images>
         <Image Id="111111" Url="http://somewhere.com/GetImage.aspx?ImageId=111111" LastModified="2016-05-12 13:09:00"/>
         <Image Id="222222" Url="http://somewhere.com/GetImage.aspx?ImageId=222222" LastModified="2016-05-12 13:09:00"/>
      </Images>
   </Vehicle>
</Stock>

新的

<Stock>
   <Vehicle id="123456">
      <Name>Ford Ka</Name>
      <Images>
         <Image Id="111111" Url="http://somewhere.com/GetImage.aspx?ImageId=111111" LastModified="2016-05-12 13:09:00"/>
         <Image Id="222222" Url="http://somewhere.com/GetImage.aspx?ImageId=222222" LastModified="2016-05-13 09:00:00"/>
         <Image Id="333333" Url="http://somewhere.com/GetImage.aspx?ImageId=333333" LastModified="2016-05-12 13:09:00"/>
      </Images>
   </Vehicle>
</Stock>

所以它们之间的区别是...

  1. 新的XMLImage Id="222222"改变了LastModified价值。
  2. 新的XML包含一个新的<Image>id="333333"

我如何使用LINQ返回一个XDocument,其中包含<Vehicle id><Image>,其中<Image id>新XML中的值不在原始XML中(差异2),或者<Image id>原始XML BUT中IS与<Image>属性值不同。同一张图片的原始XML(差异1)?

结果XDocument应该看起来像这样...

<Stock>
   <Vehicle id="123456">
      <Images>
         <Image Id="222222" Url="http://somewhere.com/GetImage.aspx?ImageId=222222" LastModified="2016-05-13 09:00:00"/>
         <Image Id="333333" Url="http://somewhere.com/GetImage.aspx?ImageId=333333" LastModified="2016-05-12 13:09:00"/>
      </Images>
   </Vehicle>
</Stock>

1]按id属性加入新旧车辆

2]查找新的或修改Image的,通过它们的字符串表示形式对其进行比较

3]选择Images进入新Vehicle元素

4]StockVehicle元素生成

var diff = from newVehicle in newXml.Descendants("Vehicle")
           join oldVehicle in oldXml.Descendants("Vehicle")
           on     newVehicle.Attribute("id").Value 
           equals oldVehicle.Attribute("id").Value 

           select new XElement("Vehicle", newVehicle.Attribute("id"),
                                new XElement("Images",
                                         newVehicle.Descendants("Image")
                                                   .Where(i=>!oldVehicle.Descendants("Image")
                                                                       .Any(iold=>iold.ToString() == i.ToString())
                                                          )
                                             )
                               );

var stock = new XElement("Stock", diff);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用 LINQ 从属性元素返回 xml 子元素

来自分类Dev

使用LINQ to XML查询元素时出错

来自分类Dev

使用LINQ解析XML以获取子元素

来自分类Dev

如何使用ObservableCollection中的LINQ查询返回元素?

来自分类Dev

LINQ to XML嵌套元素查询

来自分类Dev

Linq to XML查询嵌套元素

来自分类Dev

Linq to XML查询返回null

来自分类Dev

使用LINQ在XML文件中搜索2个不同的元素

来自分类Dev

检索不同的子元素xml

来自分类Dev

LINQ根据与子元素的匹配返回元素

来自分类Dev

如何使用LINQ获取XML中的子元素值

来自分类Dev

如何使用LINQ获取XML中的子元素值

来自分类Dev

XML上的Linq查询仅返回第一个元素

来自分类Dev

使用子查询对Linq进行SQL查询

来自分类Dev

在linq查询中使用子查询

来自分类Dev

LINQ to XML查询。获取元素属性

来自分类Dev

基于2个元素的XML linq查询

来自分类Dev

用于XML解析的Linq返回null元素

来自分类Dev

用于XML解析的Linq返回null元素

来自分类Dev

LINQ查询将父元素和子元素分组

来自分类Dev

Linq如何使用具有返回原始值(例如整数)的max函数执行子查询?

来自分类Dev

使用LINQ To XML定制XML元素

来自分类Dev

如何使用Linq To XML获取多个元素并以不同的方式存储它们?

来自分类Dev

C# 使用 LINQ 解析 xml 文档,其中存在不同数量的重复元素

来自分类Dev

如何使用一个Linq查询获取XML属性和元素?

来自分类Dev

使用LINQ在XML中查询具有相同名称的嵌套元素

来自分类Dev

尝试使用 Linq 查询时出现问题。找不到 xml 元素

来自分类Dev

Linq查询与子查询

来自分类Dev

如何使用LINQ to XML连接具有相同名称值的所有子元素

Related 相关文章

热门标签

归档