Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中通过节点名称和属性名称比较XML#_C#_Xml_Xmldiff - Fatal编程技术网

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

C# 在C中通过节点名称和属性名称比较XML#,c#,xml,xmldiff,C#,Xml,Xmldiff,我想通过标记名和属性名比较两个(或更多)XMLs文件。我对属性或节点的值不感兴趣 在谷歌上搜索我找到了XMLDiff补丁(),但它对我不起作用。。。或者我不知道如何为自己设置。 归档 1. 1. 试验 1. 测验 2. 20110910 B试验 文件B <info> <Retrieve> <LastNameInfo> <LNameNum attribute="other_val">4</LNameNum>

我想通过标记名和属性名比较两个(或更多)XMLs文件。我对属性或节点的值不感兴趣

在谷歌上搜索我找到了XMLDiff补丁(),但它对我不起作用。。。或者我不知道如何为自己设置。 归档


1.
1.
试验
1.
测验
2.
20110910
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>

4.
1.
测试7
1.
测验
2.
20110910
B试验
这两个文件必须相等


谢谢

如果您想“手动”执行此操作,可以使用递归函数并在xml结构中循环。下面是一个简单的例子:

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;
}
private bool具有不同的结构(
XmlNodeList XmlNodeList,XmlNodeList xmlNodeListB)
{
if(xmlNodeListA.Count!=xmlNodeListB.Count)返回true;
对于(变量i=0;i
请记住,只要节点和属性的顺序相同,并且在两个xml文件上使用相同的大小写,这将只返回true。可以对其进行增强以包括或排除属性/节点


希望有帮助

你只需要知道它们是否不同,或者你也需要知道它们之间的差异吗?@Lloyd,Linq有函数DeepEquals,但如果属性值或节点值不同,则返回fals.@Reinaldo,就看它们是否不同。你说Xmldiffpatch.exe对你不起作用是什么意思?也许我不知道如何设置xmldiffpoptions来忽略属性和节点的值。我希望直到最后一刻我都不需要手动执行这些操作。我知道这个解决方案,但我希望能找到一个图书馆。非常感谢你!
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;
}