2013-04-10 24 views
3

我使用XMLdiffpatch工具來檢測兩個XML文件之間變化的節點名稱,該工具的輸出XML文件看起來像這樣:獲取在輸出中的DiffGram,而不是他們的索引

<?xml version="1.0" encoding="utf-16"?> 
<xd:xmldiff version="1.0" srcDocHash="5708212576896487287" options="None" fragments="no" xmlns:xd="http://www.microsoft.com/xmldiff"> 
    <xd:node match="2"> 
     <xd:node match="3"/> 
     <xd:add> 
      <e>Some text 4</e> 
      <f>Some text 5</f> 
     </xd:add> 
     <xd:node match="4"> 
      <xd:change match="1">Changed text</xd:change> 
      <xd:remove match="2"/> 
     </xd:node> 
     <xd:node match="5"> 
      <xd:remove match="@secondAttr"/> 
      <xd:add type="2" name="newAttr">new value</xd:add> 
      <xd:change match="@firstAttr">changed attribute value</xd:change> 
     </xd:node> 
     <xd:remove match="6" opid="1"/> 
     <xd:add type="1" name="p"> 
      <xd:add type="1" name="q"> 
       <xd:add match="/2/6" opid="1"/> 
      </xd:add> 
     </xd:add> 
    </xd:node> 
    <xd:descriptor opid="1" type="move"/> 
</xd:xmldiff> 

第一個文件:

<?xml version="1.0"?> 
<b> 
    <a>Some text 1</a> 
    <b>Some text 2</b> 
    <c>Some text 3</c> 
    <d> 
    Another text 
    <foo/> 
    </d> 
    <x firstAttr="value1" secondAttr="value2"/> 
    <y> 
    <!--Any comments?--> 
    <z id="10">Just another text</z> 
    </y> 
</b> 

第二個文件:

<?xml version="1.0"?> 
<b> 
    <a>Some text 1</a> 
    <b>Some text 2</b> 
    <c>Some text 3</c> 
    <e>Some text 4</e> 
    <f>Some text 5</f> 
    <d>Changed text</d> 
    <x firstAttr="changed attribute value" newAttr="new value"/> 
    <p> 
    <q> 
     <y> 
     <!--Any comments?--> 
     <z id="10">Just another text</z> 
     </y> 
    </q> 
    </p> 
</b> 

如你所見,在xml顯示檢測到的節點改變其索引對應於其父節點。 我現在面臨的問題是如何解析這些索引,以便將它們替換爲原始xml文件中的實際節點名稱。

+0

是的,一個c#控制檯應用程序 – 2013-04-10 08:29:37

+0

您需要發佈兩個XML文件,其中輸入到XMLDiffPatch作爲數據沒有意義,否則。 – TheKingDave 2013-04-10 08:35:37

+0

非常抱歉沒有這樣做,從第一次!我上傳他們,編輯我的原始帖子 – 2013-04-10 09:11:42

回答

2

您在xml diff中看到的'匹配'數字是子節點的相對索引。整個xml diff是爲了從第一個文件構建第二個文件而構建的。在你的榜樣,

<xd:node match="2"> 
    <xd:node match="3"/> 
    <xd:add> 
     <e>Some text 4</e> 
     <f>Some text 5</f> 
    </xd:add> 

將意味着:

「在第一個文件中,找到從根本老二節點」 - 即B後節點 <>< XML>聲明?

「以找到的節點,找到第三子節點」 - 即< C>某些文本3 </C>

「下面的文本中找到的節點,插入後」 - 插入節點e和f。

MSDN上有關於xmldiff format的很詳細的文章,有一些代碼示例和xmldiff語言規範。

因此,爲了用實際值替換索引,您需要根據diff索引來遍歷源文檔,並提取真正的節點名稱。 This question有很好的代碼示例來遍歷子節點。

相關問題