diff --git a/src/DocumentFormat.OpenXml.Framework/EnumerableExtensions.cs b/src/DocumentFormat.OpenXml.Framework/EnumerableExtensions.cs deleted file mode 100644 index c13d46d06..000000000 --- a/src/DocumentFormat.OpenXml.Framework/EnumerableExtensions.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Generic; -using System.Linq; - -namespace DocumentFormat.OpenXml; - -internal static class EnumerableExtensions -{ - /// - /// Similar to but will also verify that at most there is one. - /// - public static T? FirstOrDefaultAndMaxOne(this IEnumerable enumerable, Func? exceptionFactory = null) - { - using var e = enumerable.GetEnumerator(); - - if (e.MoveNext()) - { - var first = e.Current; - - if (e.MoveNext()) - { - throw exceptionFactory?.Invoke() ?? throw new InvalidOperationException(ExceptionMessages.FirstOrDefaultMaxOne); - } - - return first; - } - - return default; - } -} diff --git a/src/DocumentFormat.OpenXml.Framework/Resources/ExceptionMessages.Designer.cs b/src/DocumentFormat.OpenXml.Framework/Resources/ExceptionMessages.Designer.cs index cf1eaed38..8419ed6c2 100644 --- a/src/DocumentFormat.OpenXml.Framework/Resources/ExceptionMessages.Designer.cs +++ b/src/DocumentFormat.OpenXml.Framework/Resources/ExceptionMessages.Designer.cs @@ -330,15 +330,6 @@ internal static string FileNotFound { } } - /// - /// Looks up a localized string similar to The enumerable contained more than a single element when only zero or one are allowed.. - /// - internal static string FirstOrDefaultMaxOne { - get { - return ResourceManager.GetString("FirstOrDefaultMaxOne", resourceCulture); - } - } - /// /// Looks up a localized string similar to The root XML element "{0}" in the part is incorrect. The expected root XML element is: "{1}".. /// diff --git a/src/DocumentFormat.OpenXml.Framework/Resources/ExceptionMessages.resx b/src/DocumentFormat.OpenXml.Framework/Resources/ExceptionMessages.resx index b0aaa58a4..171275b81 100644 --- a/src/DocumentFormat.OpenXml.Framework/Resources/ExceptionMessages.resx +++ b/src/DocumentFormat.OpenXml.Framework/Resources/ExceptionMessages.resx @@ -117,9 +117,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - The part cannot be added here. - Cannot change the document type. @@ -411,7 +408,7 @@ Package could not be opened for stream. See inner exception for details and be aware that there are behavior differences in stream support between .NET Framework and Core. - - The enumerable contained more than a single element when only zero or one are allowed. + + The part cannot be added here. \ No newline at end of file diff --git a/src/DocumentFormat.OpenXml.Framework/Validation/Semantic/RelationshipTypeConstraint.cs b/src/DocumentFormat.OpenXml.Framework/Validation/Semantic/RelationshipTypeConstraint.cs index 2e04de7dc..dd0efffbc 100644 --- a/src/DocumentFormat.OpenXml.Framework/Validation/Semantic/RelationshipTypeConstraint.cs +++ b/src/DocumentFormat.OpenXml.Framework/Validation/Semantic/RelationshipTypeConstraint.cs @@ -64,7 +64,7 @@ public RelationshipTypeConstraint(OpenXmlQualifiedName attribute, string type) { var pair = current.Part.Parts .Where(p => p.RelationshipId == attribute.Value.InnerText) - .FirstOrDefaultAndMaxOne(); + .SingleOrDefault(); if (pair is { }) { diff --git a/src/DocumentFormat.OpenXml.Framework/Validation/Semantic/SemanticConstraint.cs b/src/DocumentFormat.OpenXml.Framework/Validation/Semantic/SemanticConstraint.cs index 6793e17ec..4ce094d37 100644 --- a/src/DocumentFormat.OpenXml.Framework/Validation/Semantic/SemanticConstraint.cs +++ b/src/DocumentFormat.OpenXml.Framework/Validation/Semantic/SemanticConstraint.cs @@ -241,6 +241,7 @@ protected static bool GetAttrNumVal(OpenXmlSimpleType attributeValue, out double CultureInfo.InvariantCulture, out value); } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1851:Possible multiple enumerations of 'IEnumerable' collection", Justification = "We're resetting the parts variable, but the analyzer doesn't realize that")] private static OpenXmlPart? GetPartThroughPartPath(IEnumerable pairs, string[] path) { var foundPart = default(OpenXmlPart); @@ -251,7 +252,7 @@ protected static bool GetAttrNumVal(OpenXmlSimpleType attributeValue, out double foundPart = parts .Where(p => p.OpenXmlPart.GetType().Name == path[i]) .Select(t => t.OpenXmlPart) - .FirstOrDefaultAndMaxOne(static () => new System.IO.FileFormatException(ValidationResources.MoreThanOnePartForOneUri)); + .SingleOrDefault(); if (foundPart is not { }) {