C# & XML- App.. Comapring two XML files in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samratreddy
    New Member
    • Jul 2008
    • 5

    C# & XML- App.. Comapring two XML files in C#

    Hi,

    I need to compare two XML files in C#. Right now I'm using the XPathNavigator to parse through each node and then compare it with the node present in the other XML. I was able to print the differences. I'm displaying the values from both the XML files in a grid view.

    The problem is if I have some extra elements in the two XML files how can I display them.

    When an extra element is found in an XML file all the next elements are shown as errors.

    Please help me soon.

    Thanks in advance.

    *************** *************** *************** ***********

    Sample XML files I'm using

    XML 1

    <?xml version="1.0" encoding="UTF-8"?>

    <PageTitle>

    <Page>

    <Id>1</Id>

    <Title>This is my testing</Title>

    <PageUrl>mybo ok /1/1.aspx</PageUrl>

    </Page>

    <Page>

    <Id>2</Id>

    <Title>This is my testing</Title>

    <PageUrl> mybook /2/2.aspx</PageUrl>

    <new>4</new>

    </Page>

    <Page>

    <Id>3</Id>

    <Title>my book</Title>

    <test>123</test>

    <PageUrl>mybo ok /4/4.aspx</PageUrl>

    </Page>

    </PageTitle>


    XML 2

    <?xml version="1.0" encoding="UTF-8"?>

    <PageTitle>

    <Page>

    <Id>12345</Id>

    <Title>This is my testing</Title>

    <PageUrl>mybo ok /1/1.aspx</PageUrl>

    <test>123</test>

    </Page>

    <Page>

    <Id>2</Id>

    <Title>This is my testing</Title>

    <PageUrl> mybook /2/2.aspx</PageUrl>

    <new>4</new>

    </Page>

    <Page>

    <Id>3</Id>

    <Title>my book</Title>

    <PageUrl>mybo ok /4/4.aspx</PageUrl>

    </Page>

    </PageTitle>
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    How exactly are you moving from node to node in each document? The solution depends on this.

    Either way, it seems like if there is a mismatch it'd be one of the following:
    Say you have node1 in doc1 and node2 in doc2. mismatch.

    Check if node1 exists in doc2.
    If so, then you probably have an extra node in doc2. Display extraNode node2
    If not, then you have a missing node in doc2. Display extraNode node1.

    Comment

    • samratreddy
      New Member
      • Jul 2008
      • 5

      #3
      Hi,

      Currently I'm using XPathNavigator and Node Iterator. Any new idea is also fine.

      I just want to compare the XML files and display any mismatch in value (eg: <test>123</test> in xml1 and <test>abc</test> in xml2) should be displayed.

      Then any extra elements/nodes present in either xml1 or xml2 should be displayed as missing nodes.

      Can you [lease help me with some sample code as i'm a newbie for C#.

      Thanks

      Comment

      Working...