在 C# 中按节点名称和属性名称比较 XML [英] Comparing XML by nodes names and attributes names in C#

查看:37
本文介绍了在 C# 中按节点名称和属性名称比较 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过标签名称和属性名称比较两个(或多个)XML 文件.我对属性或节点的值不感兴趣.

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.

在谷歌上搜索我发现 XMLDiff Patch ( http://msdn.microsoft.com/en-us/library/aa302294.aspx ),但它对我不起作用......或者我不知道如何进行设置以适合我.文件 A

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>

文件 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>

这两个文件必须相等.

谢谢!

推荐答案

好吧,如果您想手动"执行此操作,一个想法是使用递归函数并循环遍历 xml 结构.这是一个简单的例子:

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());

这是你的递归函数:

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;
}

请记住,只要节点和属性的顺序相同,并且两个 xml 文件使用相同的大小写,这只会返回 true.您可以增强它以包含或排除属性/节点.

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.

希望有帮助!

这篇关于在 C# 中按节点名称和属性名称比较 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆