/// <summary>Recursively removes duplicate namespace attributes from the specified <paramref name="element"/>.</summary> /// <param name="element">Element to remove duplicate attributes from.</param> internal static void RemoveDuplicateNamespaceAttributes(System.Xml.Linq.XElement element) { Debug.Assert(element != null, "element != null"); // Nesting XElements through our client wrappers may produce duplicate namespaces. // Might need an XML fix, but won't happen for 3.5 for back compat. HashSet <string> names = new HashSet <string>(EqualityComparer <string> .Default); foreach (System.Xml.Linq.XElement e in element.DescendantsAndSelf()) { bool attributesFound = false; foreach (var attribute in e.Attributes()) { if (!attributesFound) { attributesFound = true; names.Clear(); } if (attribute.IsNamespaceDeclaration) { string localName = attribute.Name.LocalName; bool alreadyPresent = names.Add(localName) == false; if (alreadyPresent) { attribute.Remove(); } } } } }
internal static void RemoveDuplicateNamespaceAttributes(System.Xml.Linq.XElement element) { Debug.Assert(element != null, "element != null"); HashSet <string> names = new HashSet <string>(EqualityComparer <string> .Default); foreach (System.Xml.Linq.XElement e in element.DescendantsAndSelf()) { bool attributesFound = false; foreach (var attribute in e.Attributes()) { if (!attributesFound) { attributesFound = true; names.Clear(); } if (attribute.IsNamespaceDeclaration) { string localName = attribute.Name.LocalName; bool alreadyPresent = names.Add(localName) == false; if (alreadyPresent) { attribute.Remove(); } } } } }