Comparing two XML files & generating a third with XMLDiff in C#

后端 未结 2 1426

I am trying to write a simple algorithm to read two XML files with the exact same nodes and structure but not necessarily the same data inside the child nodes and not the sa

2条回答
  •  生来不讨喜
    2020-12-23 12:56

    There are two immediate solutions:

    Solution 1.

    You can first apply a simple transform to the two documents that will delete the elements that should not be compared. Then, compare the results ing two documents -- exactly with your current code. Here is the transformation:

    
     
     
    
     
      
       
      
     
    
     
    
    

    When this transformation is applied to the provided XML document:

    
        
            Sidney Crosby
            PIT
            C
            39
            32
            33
            20
            29
            10
            1
            3
            0
            154
            20.8
            21:54
            22.6
        
    
    

    the wanted resulting document is produced:

    
       
          39
          32
          33
          20
          29
          10
          1
          3
          0
          154
          20.8
          21:54
          22.6
       
    
    

    Solution 2.

    This is a complete XSLT 1.0 solution (for convenience only, the second XML document is embedded in the transformation code):

    
     
     
    
     
      
        
            John Smith
            NY
            D
            38
            32
            33
            15
            29
            10
            1
            4
            0
            158
            20.8
            21:54
            22.6
        
      
     
    
     
    
     
      
      
       
        
       
      
     
    
     
      
       
      
    
      -----------------------
    
      
       
      
     
    
     
      
      
       
      
     
    
     
    
    

    when this transformation is applied on the same first document as above, the correct diffgrams are produced:

    
       
          39
          20
          3
          154
       
    
    
      -----------------------
    
      
       
          38
          15
          4
          158
       
    
    

    How this works:

    1. The transformation is applied on the first document, passing the second document as parameter.

    2. This produces an XML document whose only leaf element nodes are the ones that have different value than the corresponding leaf element nodes in the second document.

    3. The same processing is performed as in 1. above, but this time on the second document, passing the first document as parameter.

    4. This produces a second diffgram: an XML document whose only leaf element nodes are the ones that have different value** than the corresponding leaf element nodes in the first document

提交回复
热议问题