From 9ac0abb554363136c332f3f3afa1fd8f61060234 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Fri, 14 Feb 2020 17:41:30 +0000 Subject: [PATCH] Xml use strongly typed struct enumerators (#32296) --- .../src/System/Xml/Dom/XmlDocument.cs | 15 +++++----- .../src/System/Xml/Dom/XmlLoader.cs | 28 ++++++++----------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlDocument.cs b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlDocument.cs index f0ac495afe06e..85cc8ab396ca5 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlDocument.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlDocument.cs @@ -4,6 +4,7 @@ using System; using System.Collections; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Text; @@ -673,19 +674,20 @@ internal void AddDefaultAttributes(XmlElement elem) SchemaElementDecl ed = GetSchemaElementDecl(elem); if (ed != null && ed.AttDefs != null) { - IDictionaryEnumerator attrDefs = ed.AttDefs.GetEnumerator(); - while (attrDefs.MoveNext()) + foreach (KeyValuePair attrDefs in ed.AttDefs) { - SchemaAttDef attdef = (SchemaAttDef)attrDefs.Value; + SchemaAttDef attdef = attrDefs.Value; if (attdef.Presence == SchemaDeclBase.Use.Default || attdef.Presence == SchemaDeclBase.Use.Fixed) { //build a default attribute and return - string attrPrefix = string.Empty; + string attrPrefix; string attrLocalname = attdef.Name.Name; string attrNamespaceURI = string.Empty; if (schInfo.SchemaType == SchemaType.DTD) + { attrPrefix = attdef.Name.Namespace; + } else { attrPrefix = attdef.Prefix; @@ -1687,10 +1689,9 @@ internal XmlAttribute GetDefaultAttribute(XmlElement elem, string attrPrefix, st SchemaElementDecl ed = GetSchemaElementDecl(elem); if (ed != null && ed.AttDefs != null) { - IDictionaryEnumerator attrDefs = ed.AttDefs.GetEnumerator(); - while (attrDefs.MoveNext()) + foreach (KeyValuePair attrDefs in ed.AttDefs) { - SchemaAttDef attdef = (SchemaAttDef)attrDefs.Value; + SchemaAttDef attdef = attrDefs.Value; if (attdef.Presence == SchemaDeclBase.Use.Default || attdef.Presence == SchemaDeclBase.Use.Fixed) { diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlLoader.cs b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlLoader.cs index 1f292851869dc..b9ec9ae703ac3 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlLoader.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlLoader.cs @@ -655,27 +655,21 @@ private void LoadDocumentType(IDtdInfo dtdInfo, XmlDocumentType dtNode) _doc.Entities = dtNode.Entities; //extract the elements which has attribute defined as ID from the element declarations - IDictionaryEnumerator elementDecls = schInfo.ElementDecls.GetEnumerator(); - if (elementDecls != null) + foreach (KeyValuePair elementDecls in schInfo.ElementDecls) { - elementDecls.Reset(); - while (elementDecls.MoveNext()) + SchemaElementDecl elementDecl = elementDecls.Value; + if (elementDecl.AttDefs != null) { - SchemaElementDecl elementDecl = (SchemaElementDecl)elementDecls.Value; - if (elementDecl.AttDefs != null) + foreach (KeyValuePair attDefs in elementDecl.AttDefs) { - IDictionaryEnumerator attDefs = elementDecl.AttDefs.GetEnumerator(); - while (attDefs.MoveNext()) + SchemaAttDef attdef = attDefs.Value; + if (attdef.Datatype.TokenizedType == XmlTokenizedType.ID) { - SchemaAttDef attdef = (SchemaAttDef)attDefs.Value; - if (attdef.Datatype.TokenizedType == XmlTokenizedType.ID) - { - //we only register the XmlElement based on their Prefix/LocalName and skip the namespace - _doc.AddIdInfo( - _doc.AddXmlName(elementDecl.Prefix, elementDecl.Name.Name, string.Empty, null), - _doc.AddAttrXmlName(attdef.Prefix, attdef.Name.Name, string.Empty, null)); - break; - } + //we only register the XmlElement based on their Prefix/LocalName and skip the namespace + _doc.AddIdInfo( + _doc.AddXmlName(elementDecl.Prefix, elementDecl.Name.Name, string.Empty, null), + _doc.AddAttrXmlName(attdef.Prefix, attdef.Name.Name, string.Empty, null)); + break; } } }