Can someone tell me what I have to change to fix this foreach loop (code below). It only runs once and the counter gives me the correct amount of attributes.
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();
}
Original the code was:
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();
}
but trying to fix it I've changed it to the code above. The foreach only removes the first attribute, but It has to remove all the attributes which are the same.
as requested the XML files, first the new 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>
and second the old 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>
This is a work in progress and the function for now will not work correctly and won't filter the emails, but it should only return the changed variable CompanyId.