0

I want to compare two (or more) XMLs files by tags names and attributes names. I`m not interested by values of attributes or nodes.

Searching on google i found XMLDiff Patch ( http://msdn.microsoft.com/en-us/library/aa302294.aspx ), but it not work for me... or i don`t know how to make settings to work for me. File A

    <info>
  <Retrieve>
    <LastNameInfo>
      <LNameNum attribute="some_val">1</LNameNum>
      <NumPeople>1</NumPeople>
      <NameType/>
      <LName>TEST</LName>
    </LastNameInfo>
    <Segment>
      <SegNum>1</SegNum>
      <Comment>A test</Comment>
    </Segment>
    <Segment>
      <SegNum>2</SegNum>
      <Dt>20110910</Dt>
      <Comment>B test</Comment>
    </Segment>
  </Retrieve>
</info>

File B

<info>
  <Retrieve>
    <LastNameInfo>
      <LNameNum attribute="other_val">4</LNameNum>
      <NumPeople>1</NumPeople>
      <NameType/>
      <LName>TEST7</LName>
    </LastNameInfo>
    <Segment>
      <SegNum>1</SegNum>
      <Comment>A test</Comment>
    </Segment>
    <Segment>
      <SegNum>2</SegNum>
      <Dt>20110910</Dt>
      <Comment>B test</Comment>
    </Segment>
  </Retrieve>
</info>

These two file must be equals.

Thanks!

Urmelinho
  • 1,927
  • 2
  • 14
  • 22
  • 1
    Do you just need to know if they are different, or do you need to know the differences as well? – Reinaldo Mar 17 '12 at 15:00
  • @ Lloyd, well Linq have function DeepEquals, but if values of attribute or values of node are different retun fals. @ Reinaldo, just if are differnt or not. – Urmelinho Mar 17 '12 at 15:01
  • What do you mean that Xmldiffpatch.exe did not work for you? – svick Mar 17 '12 at 15:10
  • maybe i don`t know how to set XmlDiffOptions to ignore values of attributes and nodes. – Urmelinho Mar 17 '12 at 15:12

1 Answers1

1

Well if you'd like to do this "manually" an idea would be to use a recursive function and loop through the xml structure. Here is a quick example:

var xmlFileA = //first xml
var xmlFileb = // second xml

var docA = new XmlDocument();
var docB = new XmlDocument();

docA.LoadXml(xmlFileA);
docB.LoadXml(xmlFileb);

var isDifferent = HaveDiferentStructure(docA.ChildNodes, docB.ChildNodes);
Console.WriteLine("----->>> isDifferent: " + isDifferent.ToString());

This is your recursive function:

private bool HaveDiferentStructure(
            XmlNodeList xmlNodeListA, XmlNodeList xmlNodeListB)
{
    if(xmlNodeListA.Count != xmlNodeListB.Count) return true;                

    for(var i=0; i < xmlNodeListA.Count; i++)
    {
         var nodeA = xmlNodeListA[i];
         var nodeB = xmlNodeListB[i];

         if (nodeA.Attributes == null)
         {
              if (nodeB.Attributes != null)
                   return true;
              else
                   continue;
         }

         if(nodeA.Attributes.Count != nodeB.Attributes.Count 
              || nodeA.Name != nodeB.Name) return true;

         for(var j=0; j < nodeA.Attributes.Count; j++)
         {
              var attrA = nodeA.Attributes[j];
              var attrB = nodeB.Attributes[j];

              if (attrA.Name != attrB.Name) return true;
          }

          if (nodeA.HasChildNodes && nodeB.HasChildNodes)
          {
              return HaveDiferentStructure(nodeA.ChildNodes, nodeB.ChildNodes);
          }
          else
          {
              return true;
          }
     }
     return false;
}

Please keep in mind that this will only return true as long as the nodes and attributes are in the same order, and same casing is used on both xml files. You can enhance it to include or exclude attributes/nodes.

Hope it helps!

Reinaldo
  • 4,556
  • 3
  • 24
  • 24
  • I hoped until the last moment that I don`t need to do this stuff manually. I knew this solution but I hoped to find a library. Thank you very much! – Urmelinho Mar 17 '12 at 16:53