public void DescendantsAndSelf_NestedObjects_ReturnsAllSubsequencesAndSelf() { var input = new[] { ModelGrammar.TokenObjectBeginUnnamed, ModelGrammar.TokenProperty("One"), ModelGrammar.TokenNull, ModelGrammar.TokenProperty("Two"), ModelGrammar.TokenArrayBeginUnnamed, ModelGrammar.TokenTrue, ModelGrammar.TokenPrimitive("2-B"), ModelGrammar.TokenPrimitive(23), ModelGrammar.TokenArrayEnd, ModelGrammar.TokenProperty("Three"), ModelGrammar.TokenObjectBeginUnnamed, ModelGrammar.TokenProperty("A"), ModelGrammar.TokenPrimitive("3-A"), ModelGrammar.TokenProperty("B"), ModelGrammar.TokenPrimitive(32), ModelGrammar.TokenProperty("C"), ModelGrammar.TokenPrimitive("3-C"), ModelGrammar.TokenObjectEnd, ModelGrammar.TokenProperty("Four"), ModelGrammar.TokenPrimitive(4), ModelGrammar.TokenObjectEnd }; var expected = new[] { new[] { ModelGrammar.TokenObjectBeginUnnamed, ModelGrammar.TokenProperty("One"), ModelGrammar.TokenNull, ModelGrammar.TokenProperty("Two"), ModelGrammar.TokenArrayBeginUnnamed, ModelGrammar.TokenTrue, ModelGrammar.TokenPrimitive("2-B"), ModelGrammar.TokenPrimitive(23), ModelGrammar.TokenArrayEnd, ModelGrammar.TokenProperty("Three"), ModelGrammar.TokenObjectBeginUnnamed, ModelGrammar.TokenProperty("A"), ModelGrammar.TokenPrimitive("3-A"), ModelGrammar.TokenProperty("B"), ModelGrammar.TokenPrimitive(32), ModelGrammar.TokenProperty("C"), ModelGrammar.TokenPrimitive("3-C"), ModelGrammar.TokenObjectEnd, ModelGrammar.TokenProperty("Four"), ModelGrammar.TokenPrimitive(4), ModelGrammar.TokenObjectEnd }, new[] { ModelGrammar.TokenNull }, new[] { ModelGrammar.TokenArrayBeginUnnamed, ModelGrammar.TokenTrue, ModelGrammar.TokenPrimitive("2-B"), ModelGrammar.TokenPrimitive(23), ModelGrammar.TokenArrayEnd }, new[] { ModelGrammar.TokenTrue }, new[] { ModelGrammar.TokenPrimitive("2-B") }, new[] { ModelGrammar.TokenPrimitive(23) }, new[] { ModelGrammar.TokenObjectBeginUnnamed, ModelGrammar.TokenProperty("A"), ModelGrammar.TokenPrimitive("3-A"), ModelGrammar.TokenProperty("B"), ModelGrammar.TokenPrimitive(32), ModelGrammar.TokenProperty("C"), ModelGrammar.TokenPrimitive("3-C"), ModelGrammar.TokenObjectEnd }, new[] { ModelGrammar.TokenPrimitive("3-A") }, new[] { ModelGrammar.TokenPrimitive(32) }, new[] { ModelGrammar.TokenPrimitive("3-C") }, new[] { ModelGrammar.TokenPrimitive(4), } }; // select all descendants and self var actual = input.DescendantsAndSelf().ToArray(); Assert.Equal(expected, actual, false); }
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(); } } } } }
/// <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(); } } } } }