foreach检查找到XDocuments之间的差异只运行一次

时间:2015-03-12 11:34:31

标签: c# xml linq

有人可以告诉我我需要更改以修复此foreach循环(下面的代码)。它只运行一次,计数器给我正确的属性数量。

public static String FindChanges(string input, string old)
    {
        XDocument newXml = XDocument.Parse(input);
        XDocument returnXML = newXml;
        XDocument oldXml = XDocument.Parse(old);
        int counter = newXml.Root.Attributes().Count();
        foreach (XAttribute check in newXml.Root.Attributes())
        {
            if (check.Value == oldXml.Root.Attribute(check.Name).Value) returnXML.Root.Attribute(check.Name).Remove();
        }
        return returnXML.ToString();
    }

原始代码是:

public static String FindChanges(string input, string old)
    {
        XDocument newXml = XDocument.Parse(input);
        XDocument oldXml = XDocument.Parse(old);
        int counter = newXml.Root.Attributes().Count();
        foreach (XAttribute check in newXml.Root.Attributes())
        {
            if (check.Value == oldXml.Root.Attribute(check.Name).Value) newXML.Root.Attribute(check.Name).Remove();
        }
        return newXML.ToString();
    }

但尝试修复它我已将其更改为上面的代码。 foreach只删除第一个属性,但它必须删除所有相同的属性。

按要求提供XML文件,首先是新的XML:

<User LoginName=\"Paul\" Owner=\"\" UserId=\"Paul\" Alias=\"Testing\" UserType=\"PAID\" ClientType=\"OBM\" Status=\"ENABLE\" Quota=\"104857600\" Timezone=\"GMT-08:00 (PST)\" Language=\"en\" DataFile=\"0\" DataSize=\"0\" RetainFile=\"0\" RetainSize=\"0\" UncompressedSize=\"0\" UncompressedRetainSize=\"0\" EnableMSSQL=\"Y\" EnableMSExchange=\"N\" MsExchangeQuota=\"0\" EnableOracle=\"N\" EnableLotusNotes=\"N\" EnableLotusDomino=\"Y\" EnableMySQL=\"Y\" EnableInFileDelta=\"N\" EnableShadowCopy=\"N\" EnableExchangeMailbox=\"Y\" ExchangeMailboxQuota=\"1\" EnableNASClient=\"Y\" EnableDeltaMerge=\"Y\" EnableMsVm=\"Y\" MsVmQuota=\"2\" EnableVMware=\"N\" VMwareQuota=\"0\" Bandwidth=\"0\" Notes=\"BLABLjbldiela!!\" UserHome=\"/ubs/module/obsr/system/obsr/user/Paul\" RegistrationDate=\"1417186638067\" MailboxUsage=\"0\" SuspendPaidUser=\"N\" SuspendPaidUserDate=\"20150212\" LastBackupDate=\"0\" EnableCDP=\"N\" EnableShadowProtectBareMetal=\"N\" EnableWinServer2008BareMetal=\"Y\" MUserId=\"5\" CompanyId=\"2\" CompanyName=\"NoCompany\">\r\n  <Contact Name=\"demo\" Email=\"demo2@home.nl\" />\r\n</User>

以及旧的XML:

<User LoginName=\"Paul\" Owner=\"\" UserId=\"Paul\" Alias=\"Testing\" UserType=\"PAID\" ClientType=\"OBM\" Status=\"ENABLE\" Quota=\"104857600\" Timezone=\"GMT-08:00 (PST)\" Language=\"en\" DataFile=\"0\" DataSize=\"0\" RetainFile=\"0\" RetainSize=\"0\" UncompressedSize=\"0\" UncompressedRetainSize=\"0\" EnableMSSQL=\"Y\" EnableMSExchange=\"N\" MsExchangeQuota=\"0\" EnableOracle=\"N\" EnableLotusNotes=\"N\" EnableLotusDomino=\"Y\" EnableMySQL=\"Y\" EnableInFileDelta=\"N\" EnableShadowCopy=\"N\" EnableExchangeMailbox=\"Y\" ExchangeMailboxQuota=\"1\" EnableNASClient=\"Y\" EnableDeltaMerge=\"Y\" EnableMsVm=\"Y\" MsVmQuota=\"2\" EnableVMware=\"N\" VMwareQuota=\"0\" Bandwidth=\"0\" Notes=\"BLABLjbldiela!!\" UserHome=\"/ubs/module/obsr/system/obsr/user/Paul\" RegistrationDate=\"1417186638067\" MailboxUsage=\"0\" SuspendPaidUser=\"N\" SuspendPaidUserDate=\"20150212\" LastBackupDate=\"0\" EnableCDP=\"N\" EnableShadowProtectBareMetal=\"N\" EnableWinServer2008BareMetal=\"Y\" MUserId=\"5\" CompanyId=\"1\" CompanyName=\"NoCompany\">\r\n  <Contact Name=\"demo\" Email=\"demo2@home.nl\" />\r\n</User>

这是正在进行的工作,现在的功能无法正常工作,也不会过滤电子邮件,但只应返回已更改的变量CompanyId。

2 个答案:

答案 0 :(得分:1)

承诺最终的代码:

/// <summary>
    /// Find the changes in 
    /// </summary>
    /// <param name="input">The new Input XML which is saved in the databases</param>
    /// <param name="old">The old XML which was loaded at the start</param>
    /// <returns>only the diffrences in XML format</returns>
    public static String FindChanges(string input, string old)
    {
        bool returnEmpty = true;
        XDocument newXml = XDocument.Parse(input);
        XDocument returnXML = XDocument.Parse(input);
        XDocument oldXml = XDocument.Parse(old);
        foreach (XAttribute check in newXml.Root.Attributes())
        {
            if (check.Value == oldXml.Root.Attribute(check.Name).Value) returnXML.Root.Attribute(check.Name).Remove();
        }
        if (returnXML.Root.HasAttributes) returnEmpty = false;
        if (newXml.Root.HasElements)
        {
            foreach (XElement sub in newXml.Root.Elements())
            {
                foreach (XAttribute check in sub.Attributes())
                {
                    if (check.Value == oldXml.Root.Element(sub.Name).Attribute(check.Name).Value) returnXML.Root.Element(sub.Name).Attribute(check.Name).Remove();
                }
                if (!returnXML.Root.Element(sub.Name).HasAttributes) returnXML.Root.Element(sub.Name).Remove();
                else returnEmpty = false;
            }
        }
        return returnEmpty? "" : returnXML.ToString();
    }

答案 1 :(得分:0)

当我们必须在循环期间修改项目时,

foreach循环永远不是一个更好的选择。我过去也遇到过这个问题。正如Sayse在他的评论中提到的那样,for循环会做(我们将不得不使用迭代器变量来显示记录)