How can I read diffgram file generated by XmlDiff.

2019-05-16 10:17发布

问题:

I am comparing two XML files using XMLDiff.compare() Which I am using for the first time and unable to figure out what to do with the diffgram file generated to generate an HTML for the Difference in the two XMLs.

bool bIdentical = xmldiff.Compare(originalFile, newFile, false, diffgramWriter);

where original file is

<?xml version="1.0" encoding="utf-8"?>
<testsOutputData>
<TestName_AK1>
    <FeatureID ID="33B50792-207E514">
        <Intance_ID_1 >
            <CommandString>ABC </CommandString>
            <ProductID>100000</ProductID>
            <ProjectName>No Project</ProjectName>
            <TaskID>TESTSUITE</TaskID>
            <Type>Local</Type>
            <User></User>
        </Intance_ID_1>
        <Intance_ID_2>
            <CommandString>PQR </CommandString>
            <ProductID>1000</ProductID>
            <ProjectName>No Project</ProjectName>
            <TaskID>TESTSUITE</TaskID>
            <Type>Local</Type>
            <User></User>
        </Intance_ID_2>
        <Intance_ID_3>
            <CommandString>ABC </CommandString>
            <ProductID>1000</ProductID>
            <ProjectName>No Project</ProjectName>
            <TaskID>TESTSUITE</TaskID>
            <Type>Local</Type>
            <User></User>
        </Intance_ID_3>
    </FeatureID>
</TestName_AK1>

And new file is

<?xml version="1.0" encoding="utf-8"?>
<testsOutputData>
<TestName_AK1>
    <FeatureID ID="33B50792-207E514">
        <Intance_ID_2>
            <CommandString>PQR </CommandString>
            <ProductID>1000</ProductID>
            <ProjectName>No Project</ProjectName>
            <TaskID>TESTSUITE</TaskID>
            <Type>Local</Type>
            <User></User>
        </Intance_ID_2>
        <Intance_ID_1 >
            <CommandString>ABC </CommandString>
            <ProductID>100000</ProductID>
            <ProjectName>No Project</ProjectName>
            <TaskID>TESTSUITE</TaskID>
            <Type>Local</Type>
            <User></User>
        </Intance_ID_1>
        <Intance_ID_3>
            <CommandString>ABC </CommandString>
            <ProductID>1000</ProductID>
            <ProjectName>No Project</ProjectName>
            <TaskID>TESTSUITE</TaskID>
            <Type>Local</Type>
            <User></User>
        </Intance_ID_3>
    </FeatureID>
</TestName_AK1>

the diffgramwriter generated is

<xd:xmldiff version="1.0" srcDocHash="11314582626391529293" options="None" fragments="no" xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff">
<xd:node match="2">
<xd:node match="1">
  <xd:node match="1">
    <xd:add match="/2/1/1/2" opid="1" />
    <xd:remove match="2" opid="1" />
  </xd:node>
</xd:node>
</xd:node>
<xd:descriptor opid="1" type="move" />
</xd:xmldiff>

now using this need to generate HTML file.(This is later part) But I am not able to understand(read) the diffgram file properly, not able to get the meaning of

<xd:add match="/2/1/1/2" opid="1" />
    <xd:remove match="2" opid="1" />

and

<xd:descriptor opid="1" type="move" />

and their are more of this type node in other diffgram for other cases. how can i understand this diffgram file in such a way that I can generate an an HTML

回答1:

You can consume your diffgram in XmlDiffView class. But for me it doesn't work perfectly. It highlights the difference but also shows that I deleted some nodes and added new.

XmlDiffView dv = new XmlDiffView();
//Load the original file again and the diff file.
XmlTextReader orig = new XmlTextReader("F:\\XML_1.xml");
        XmlTextReader diffGram = new XmlTextReader("F:\\diff.xml");
        dv.Load(orig,
            diffGram);

        //Wrap the HTML file with necessary html and 
        //body tags and prepare it before passing it to the GetHtml method.

        string tempFile =  "F:\\diff" +r.Next() + ".htm";
        StreamWriter sw1 = new StreamWriter(tempFile);
        sw1.Write("<html><body><table width='100%'>");
        //Write Legend.
        sw1.Write("<tr><td colspan='2' align='center'><b>Legend:</b> <font style='background-color: yellow'" +
            " color='black'>added</font>&nbsp;&nbsp;<font style='background-color: red'" +
            " color='black'>removed</font>&nbsp;&nbsp;<font style='background-color: " +
            "lightgreen' color='black'>changed</font>&nbsp;&nbsp;" +
            "<font style='background-color: red' color='blue'>moved from</font>" +
            "&nbsp;&nbsp;<font style='background-color: yellow' color='blue'>moved to" +
            "</font>&nbsp;&nbsp;<font style='background-color: white' color='#AAAAAA'>" +
            "ignored</font></td></tr>");

        dv.GetHtml(sw1);
        sw1.Write("</table></body></html>");
        sw1.Close();
        dv = null;
        orig.Close();
        diffGram.Close();

I ran this using your xml files and they are equal. When I modified the value CommandString from ABC to ABCD I got highligted changed value.