简体   繁体   English

通过C#中的节点名称和属性名称比较XML

[英]Comparing XML by nodes names and attributes names in C#

I want to compare two (or more) XMLs files by tags names and attributes names. 我想通过标签名称和属性名称来比较两个(或多个)XML文件。 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. 在Google上搜索时,我发现了XMLDiff补丁( http://msdn.microsoft.com/zh-cn/library/aa302294.aspx ),但是它对我不起作用...或者我不知道如何进行设置才能正常工作为了我。 File A 文件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 文件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! 谢谢!

Well if you'd like to do this "manually" an idea would be to use a recursive function and loop through the xml structure. 好吧,如果您想“手动”执行此操作,则可以使用递归函数并遍历xml结构。 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. 请记住,仅当节点和属性的顺序相同且两个xml文件使用相同的大小写时,此方法才返回true。 You can enhance it to include or exclude attributes/nodes. 您可以增强它以包括或排除属性/节点。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM