Get the node names in the ouput diffgram instead of their indexes

c#, xml

Solution

The 'match' numbers you see in xml diff are relative indices of child nodes. Entire xml diff is built to construct second file from first file. In your example,

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

Would mean:

"In first file, locate second child node from root" - that is, node <b> after <?xml> declaration.

"In found node, locate third child node" - that is <c>Some text 3</c>

"After found node, insert following text" - insert nodes e and f.

There is a great detailed article about xmldiff format on MSDN, with some code samples and xmldiff language spec.

So in order to replace indices with real values, you would need to traverse source document according to diff indices, and extract real node names. This question has nice code sample to traverse child nodes.

Problem

I'm using `XMLdiffpatch` tool to detect changes between two xml files , the output xml file of the tool looks like this : ``` <?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> ``` First File : ``` <?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> ``` Second file : ``` <?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> ``` as you see , the xml displays the detected node changes upon their index corresponding to their parent nodes . The problem I'm facing now is how to parse these indexes , in order to replace them with their actual node names in the original xml file .

Original source