我試圖審計一些定製的軟件中使用的XML。我能夠使用「XNode.DeepEquals」檢測相同結構中的更改,然後向已更改的元素添加額外的屬性,以便突出顯示它們。使用Linq和XElement檢測XML中的結構差異
我的問題是,當結構確實改變這種方法失敗。 (我在同一時間枚舉在兩個XElements執行DeepEquals,如果他們不相等 - 遞歸調用同樣的方法來過濾掉其中的確切變化occurr)
這顯然現在分崩離析當我列舉和被比較的節點是不一樣的。請參見下面的示例:
以前
<?xml version="1.0" encoding="utf-16"?>
<Prices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Price default="true">
<Expression operator="Addition">
<LeftOperand>
<AttributeValue field="ccx_bandwidth" />
</LeftOperand>
<RightOperand>
<Constant value="10" type="Integer" />
</RightOperand>
</Expression>
</Price>
<Price default="false">
<Expression operator="Addition">
<LeftOperand>
<AttributeValue field="ccx_bandwidth" />
</LeftOperand>
<RightOperand>
<Constant value="99" type="Integer" />
</RightOperand>
</Expression>
</Price>
<RollupChildren />
後
<?xml version="1.0" encoding="utf-16"?>
<Prices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Price default="true">
<Expression operator="Addition">
<LeftOperand>
<AttributeValue field="ccx_bandwidth" />
</LeftOperand>
<RightOperand>
<Constant value="10" type="Integer" />
</RightOperand>
</Expression>
</Price>
<RollupChildren />
所以你可以看到,後者的價格具有節點已被刪除,我需要顯示此更改。
目前我可以訪問這兩個XML片段,並在審計應用程序的負載上使用'silverchanged'屬性修改它們,在我的Silverlight應用程序中,我使用轉換器綁定背景。
我一直在玩Linq到Xml,看着加入查詢中的兩個XElements,但不知道如何繼續。
理想情況下,我想要做的是合併兩個XElements在一起,但添加一個單獨的屬性取決於它是否被添加或刪除,然後我可以綁定到一個轉換器說適當的紅色或綠色突出顯示。
有沒有人對此有過任何明智的想法? (我一直在看XmlDiff,但是我不能在Silverlight中使用它,我不認爲?)
我已經試過以上一個簡單的例子,它的要求並不完全工作,因爲它似乎認爲所有的事情被刪除,如果有些事情不太地方預計。好主意,所以我可以玩這個更多 – Chris 2012-02-16 19:49:32
嗯,我試了兩個問題的例子,它的工作是正確的,即標記第二個價格標籤和它的後代被刪除。 – user1096188 2012-02-17 06:33:56