From bd3717166b88dbc0abca58ab7d3f92736906e233 Mon Sep 17 00:00:00 2001 From: "Kasper F. Brandt" Date: Wed, 4 Apr 2018 22:22:56 +0200 Subject: [PATCH] TotalItemsInView in ResolutionSet is not reliable, workaround for #177. --- Misc/NameResolution.cs | 11 +++++++++-- Misc/NameResolutionCollection.cs | 12 +++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Misc/NameResolution.cs b/Misc/NameResolution.cs index 2f9de1e9..7010de63 100644 --- a/Misc/NameResolution.cs +++ b/Misc/NameResolution.cs @@ -56,9 +56,15 @@ internal NameResolution(NameResolutionCollection owner) /// Loads from XML. /// /// The reader. - internal void LoadFromXml(EwsServiceXmlReader reader) + /// If true the function returns false rather than throw an exception + /// if the last element has been read. + internal bool LoadFromXml(EwsServiceXmlReader reader, bool allowNoMoreElements) { - reader.ReadStartElement(XmlNamespace.Types, XmlElementNames.Resolution); + reader.Read(); + if (allowNoMoreElements && reader.NodeType == System.Xml.XmlNodeType.EndElement) + return false; + + reader.EnsureCurrentNodeIsStartElement(XmlNamespace.Types, XmlElementNames.Resolution); reader.ReadStartElement(XmlNamespace.Types, XmlElementNames.Mailbox); this.mailbox.LoadFromXml(reader, XmlElementNames.Mailbox); @@ -81,6 +87,7 @@ internal void LoadFromXml(EwsServiceXmlReader reader) { reader.EnsureCurrentNodeIsEndElement(XmlNamespace.Types, XmlElementNames.Resolution); } + return true; } /// diff --git a/Misc/NameResolutionCollection.cs b/Misc/NameResolutionCollection.cs index 7fdc1f08..bd43225c 100644 --- a/Misc/NameResolutionCollection.cs +++ b/Misc/NameResolutionCollection.cs @@ -60,19 +60,21 @@ internal void LoadFromXml(EwsServiceXmlReader reader) { reader.ReadStartElement(XmlNamespace.Messages, XmlElementNames.ResolutionSet); - int totalItemsInView = reader.ReadAttributeValue(XmlAttributeNames.TotalItemsInView); + // Note: TotalItemsInView is not reliable, https://github.com/OfficeDev/ews-managed-api/issues/177 this.includesAllResolutions = reader.ReadAttributeValue(XmlAttributeNames.IncludesLastItemInRange); - for (int i = 0; i < totalItemsInView; i++) + NameResolution nameResolution; + while (true) { - NameResolution nameResolution = new NameResolution(this); + nameResolution = new NameResolution(this); - nameResolution.LoadFromXml(reader); + if (!nameResolution.LoadFromXml(reader, true)) + break; this.items.Add(nameResolution); } - reader.ReadEndElement(XmlNamespace.Messages, XmlElementNames.ResolutionSet); + reader.EnsureCurrentNodeIsEndElement(XmlNamespace.Messages, XmlElementNames.ResolutionSet); } ///