【问题标题】:How to compare two big XML files item by item efficiently?如何有效地逐项比较两个大 XML 文件?
【发布时间】:2010-12-02 09:54:45
【问题描述】:

我计划实现一种方法来比较两个大型 XML 文件(但每个文件的元素行少于 10,000 行)。

下面的方法有效,但文件超过100行时效果不佳。它开始非常缓慢。我怎样才能找到更有效的解决方案。可能需要高级 C# 编程设计或更好的 C# 和 XML 处理算法。

提前感谢您的 cmets。

//Remove the item which not in Event Xml and ConfAddition Xml files
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile_AlarmSettingUp);

bool isNewAlid_Event = false;
bool isNewAlid_ConfAddition = false;
int alid = 0;

XmlNodeList xnList = doc.SelectNodes("/Equipment/AlarmSettingUp/EnabledALIDs/ALID");

foreach (XmlNode xn in xnList)
{                        
    XmlAttributeCollection attCol = xn.Attributes;

    for (int i = 0; i < attCol.Count; ++i)
    {
        if (attCol[i].Name == "alid")
        {
            alid = int.Parse(attCol[i].Value.ToString());
            break;
        }
    }

    //alid = int.Parse(attCol[1].Value.ToString());

    XmlDocument docEvent_Alarm = new XmlDocument();
    docEvent_Alarm.Load(xmlFile_Event);
    XmlNodeList xnListEvent_Alarm = docEvent_Alarm.SelectNodes("/Equipment/Alarms/ALID");
    foreach (XmlNode xnEvent_Alarm in xnListEvent_Alarm)
    {
        XmlAttributeCollection attColEvent_Alarm = xnEvent_Alarm.Attributes;
        int alidEvent_Alarm = int.Parse(attColEvent_Alarm[1].Value.ToString());
        if (alid == alidEvent_Alarm)
        {
            isNewAlid_Event = false;
            break;
        }
        else
        {
            isNewAlid_Event = true;
            //break;
        }
    }

    XmlDocument docConfAddition_Alarm = new XmlDocument();
    docConfAddition_Alarm.Load(xmlFile_ConfAddition);
    XmlNodeList xnListConfAddition_Alarm = docConfAddition_Alarm.SelectNodes("/Equipment/Alarms/ALID");
    foreach (XmlNode xnConfAddition_Alarm in xnListConfAddition_Alarm)
    {
        XmlAttributeCollection attColConfAddition_Alarm = xnConfAddition_Alarm.Attributes;
        int alidConfAddition_Alarm = int.Parse(attColConfAddition_Alarm[1].Value.ToString());
        if (alid == alidConfAddition_Alarm)
        {
            isNewAlid_ConfAddition = false;
            break;
        }
        else
        {
            isNewAlid_ConfAddition = true;
            //break;
        }
    }                        

    if ( isNewAlid_Event && isNewAlid_ConfAddition )
    {
        // Store the root node of the destination document into an XmlNode
        XmlNode rootDest = doc.SelectSingleNode("/Equipment/AlarmSettingUp/EnabledALIDs");
        rootDest.RemoveChild(xn);
    }

}
doc.Save(xmlFile_AlarmSettingUp);

我的 XML 文件就是这样。这两个 XML 文件的样式相同。除了有时我的应用程序可能会修改其中之一。这就是为什么我需要在修改时比较它们。

<?xml version="1.0" encoding="utf-8"?>
<Equipment xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Licence LicenseId="" LicensePath="" />
  <!--Alarm Setting Up XML File-->
  <AlarmSettingUp>
    <EnabledALIDs>
      <ALID logicalName="Misc_EV_RM_STATION_ALREADY_RESERVED" alid="536870915" alcd="7" altx="Misc_Station  1   UnitName  2   SlotId already reserved" ceon="Misc_AlarmOn_EV_RM_STATION_ALREADY_RESERVED" ceoff="Misc_AlarmOff_EV_RM_STATION_ALREADY_RESERVED" />
      <ALID logicalName="Misc_EV_RM_SEQ_READ_ERROR" alid="536870916" alcd="7" altx="Misc_Sequence ID  1 d step  2 d read error for wafer in  3   UnitName  4   SlotId" ceon="Misc_AlarmOn_EV_RM_SEQ_READ_ERROR" ceoff="Misc_AlarmOff_EV_RM_SEQ_READ_ERROR" />
...
...
...
    </EnabledALIDs>
  </AlarmSettingUp>
</Equipment>

【问题讨论】:

  • 呃,当您尝试对性能进行基准测试时,嵌套迭代不是要走的路。清理你的代码。

标签: c# .net xml c#-2.0 compare


【解决方案1】:

“ALID/@alid”似乎是你的关键,所以我要做的第一件事(在foreach (XmlNode xn in xnList) 之前)是在docEvent_Alarm.SelectNodes("/Equipment/Alarms/ALID") @alid 值上构建一个字典(假设这是唯一的) - 然后你可以在没有 O(n*m) 性能的情况下完成大部分工作 - 它会更多 O(n+m)(这是一个很大的区别)。

var lookup = new Dictionary<string, XmlElement>();
foreach(XmlElement el in docEvent_Alarm.SelectNodes("/Equipment/Alarms/ALID")) {
    lookup.Add(el.GetAttribute("alid"), el);
}

那么你可以使用:

XmlElement other;
if(lookup.TryGetValue(otherKey, out other)) {
   // exists; element now in "other"
} else {
   // doesn't exist
}

【讨论】:

  • var 来自.net 2.0?我必须基于.net 2.0。
  • @Nano HE,然后简单地将 var 替换为类型名称,在这种情况下为 Dictionary&lt;string, XmlElement&gt;
  • @Nano - var 是 C# 3.0,可用于针对任何 .NET 版本;但@steiner 是对的;如果使用 C# 2.0 (VS2005) 只需使用 Dictionary&lt;string,XmlElement&gt;
【解决方案2】:

XmlDocument 和相关类(XmlNode,...)在 xml 处理中并不是很快。请改用 XmlTextReader。

您还调用docEvent_Alarm.Load(xmlFile_Event);docConfAddition_Alarm.Load(xmlFile_ConfAddition); 父母循环的每次迭代 - 这不好。如果您的 xmlFile_EventxmlFile_ConfAddition 在所有处理过程中都保持不变 - 最好在主循环之前对其进行初始化。

【讨论】:

    【解决方案3】:

    您是否尝试过使用 Microsoft 的 XmlDiff 类?见http://msdn.microsoft.com/en-us/library/aa302294.aspx

    【讨论】:

      猜你喜欢
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 2017-12-31
      • 2016-03-06
      • 2020-04-26
      相关资源
      最近更新 更多