Add nullable annotations for XmlDataDocument (#41541)
authorImmo Landwerth <immol@microsoft.com>
Tue, 1 Sep 2020 01:09:41 +0000 (18:09 -0700)
committerGitHub <noreply@github.com>
Tue, 1 Sep 2020 01:09:41 +0000 (18:09 -0700)
* Add nullable annotations for XmlDataDocument

* Address code review feedback

* More code review feedback

* Address Eric's feedback

* Annotate _dataSet as not null when AttachDataSet is called

* Fix GetElementById return type annotation

In order to match with src

Co-authored-by: Jeff Handley <jeff.handley@microsoft.com>
Co-authored-by: David CantĂș <dacantu@microsoft.com>
12 files changed:
src/libraries/System.Data.Common/ref/System.Data.Common.cs
src/libraries/System.Data.Common/src/System/Xml/BaseTreeIterator.cs
src/libraries/System.Data.Common/src/System/Xml/DataDocumentXPathNavigator.cs
src/libraries/System.Data.Common/src/System/Xml/DataPointer.cs
src/libraries/System.Data.Common/src/System/Xml/DataSetMappper.cs
src/libraries/System.Data.Common/src/System/Xml/IXmlDataVirtualNode.cs
src/libraries/System.Data.Common/src/System/Xml/RegionIterator.cs
src/libraries/System.Data.Common/src/System/Xml/TreeIterator.cs
src/libraries/System.Data.Common/src/System/Xml/XPathNodePointer.cs
src/libraries/System.Data.Common/src/System/Xml/XmlBoundElement.cs
src/libraries/System.Data.Common/src/System/Xml/XmlDataDocument.cs
src/libraries/System.Data.Common/src/System/Xml/XmlDataImplementation.cs

index d567c02..537b12b 100644 (file)
@@ -3536,7 +3536,6 @@ namespace System.Data.SqlTypes
         UnmanagedBuffer = 2,
     }
 }
-#nullable disable
 namespace System.Xml
 {
     [System.ObsoleteAttribute("XmlDataDocument class will be removed in a future release.")]
@@ -3546,17 +3545,16 @@ namespace System.Xml
         public XmlDataDocument(System.Data.DataSet dataset) { }
         public System.Data.DataSet DataSet { get { throw null; } }
         public override System.Xml.XmlNode CloneNode(bool deep) { throw null; }
-        public override System.Xml.XmlElement CreateElement(string prefix, string localName, string namespaceURI) { throw null; }
+        public override System.Xml.XmlElement CreateElement(string? prefix, string localName, string? namespaceURI) { throw null; }
         public override System.Xml.XmlEntityReference CreateEntityReference(string name) { throw null; }
-        protected override System.Xml.XPath.XPathNavigator CreateNavigator(System.Xml.XmlNode node) { throw null; }
-        public override System.Xml.XmlElement GetElementById(string elemId) { throw null; }
+        protected override System.Xml.XPath.XPathNavigator? CreateNavigator(System.Xml.XmlNode node) { throw null; }
+        public override System.Xml.XmlElement? GetElementById(string elemId) { throw null; }
         public System.Xml.XmlElement GetElementFromRow(System.Data.DataRow r) { throw null; }
         public override System.Xml.XmlNodeList GetElementsByTagName(string name) { throw null; }
-        public System.Data.DataRow GetRowFromElement(System.Xml.XmlElement e) { throw null; }
+        public System.Data.DataRow? GetRowFromElement(System.Xml.XmlElement? e) { throw null; }
         public override void Load(System.IO.Stream inStream) { }
         public override void Load(System.IO.TextReader txtReader) { }
         public override void Load(string filename) { }
         public override void Load(System.Xml.XmlReader reader) { }
     }
 }
-#nullable enable
index 74a1721..ed8b41d 100644 (file)
@@ -1,8 +1,8 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-// TODO: Enable after System.Private.Xml is annotated
-#nullable disable
+using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
 
 namespace System.Xml
 {
@@ -16,11 +16,15 @@ namespace System.Xml
             this.mapper = mapper;
         }
 
-        internal abstract XmlNode CurrentNode { get; }
+        internal abstract XmlNode? CurrentNode { get; }
 
+        [MemberNotNullWhen(true, nameof(CurrentNode))]
         internal abstract bool Next();
+
+        [MemberNotNullWhen(true, nameof(CurrentNode))]
         internal abstract bool NextRight();
 
+        [MemberNotNullWhen(true, nameof(CurrentNode))]
         internal bool NextRowElement()
         {
             while (Next())
@@ -33,6 +37,7 @@ namespace System.Xml
             return false;
         }
 
+        [MemberNotNullWhen(true, nameof(CurrentNode))]
         internal bool NextRightRowElement()
         {
             if (NextRight())
@@ -47,10 +52,10 @@ namespace System.Xml
         }
 
         // Returns true if the current node is on a row element (head of a region)
+        [MemberNotNullWhen(true, nameof(CurrentNode))]
         internal bool OnRowElement()
         {
-            XmlBoundElement be = CurrentNode as XmlBoundElement;
-            return (be != null) && (be.Row != null);
+            return CurrentNode is XmlBoundElement be && be.Row != null;
         }
     }
 }
index ec88cf9..1fc9394 100644 (file)
@@ -1,9 +1,6 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-// TODO: Enable after System.Private.Xml is annotated
-#nullable disable
-
 using System.Xml.XPath;
 
 #pragma warning disable 0618 // ignore obsolete warning about XmlDataDocument
@@ -50,7 +47,7 @@ namespace System.Xml
             get
             {
                 XPathNodeType xnt = _curNode.NodeType;
-                return xnt == XPathNodeType.Element || xnt == XPathNodeType.Root ? _curNode.InnerText : _curNode.Value;
+                return xnt == XPathNodeType.Element || xnt == XPathNodeType.Root ? _curNode.InnerText : _curNode.Value!;
             }
         }
 
@@ -73,7 +70,7 @@ namespace System.Xml
             }
 
             _temp.MoveTo(_curNode);
-            return _temp.MoveToAttribute(localName, namespaceURI) ? _temp.Value : string.Empty;
+            return _temp.MoveToAttribute(localName, namespaceURI) ? _temp.Value! : string.Empty;
         }
 
         public override string GetNamespace(string name) => _curNode.GetNamespace(name);
@@ -129,7 +126,7 @@ namespace System.Xml
         {
             if (other != null)
             {
-                DataDocumentXPathNavigator otherDataDocXPathNav = other as DataDocumentXPathNavigator;
+                DataDocumentXPathNavigator? otherDataDocXPathNav = other as DataDocumentXPathNavigator;
                 if (otherDataDocXPathNav != null && _curNode.MoveTo(otherDataDocXPathNav.CurNode))
                 {
                     _doc = _curNode.Document;
@@ -146,7 +143,7 @@ namespace System.Xml
         {
             if (other != null)
             {
-                DataDocumentXPathNavigator otherDataDocXPathNav = other as DataDocumentXPathNavigator;
+                DataDocumentXPathNavigator? otherDataDocXPathNav = other as DataDocumentXPathNavigator;
                 if (otherDataDocXPathNav != null &&
                     _doc == otherDataDocXPathNav.Document && _curNode.IsSamePosition(otherDataDocXPathNav.CurNode))
                 {
@@ -158,16 +155,16 @@ namespace System.Xml
 
         //the function is only called for XPathNodeList enumerate nodes and
         // shouldn't be promoted to frequently use because it will cause foliation
-        XmlNode IHasXmlNode.GetNode() => _curNode.Node;
+        XmlNode IHasXmlNode.GetNode() => _curNode.Node!;
 
-        public override XmlNodeOrder ComparePosition(XPathNavigator other)
+        public override XmlNodeOrder ComparePosition(XPathNavigator? other)
         {
             if (other == null)
             {
                 return XmlNodeOrder.Unknown; // this is what XPathDocument does.
             }
 
-            DataDocumentXPathNavigator otherDataDocXPathNav = other as DataDocumentXPathNavigator;
+            DataDocumentXPathNavigator? otherDataDocXPathNav = other as DataDocumentXPathNavigator;
 
             return otherDataDocXPathNav == null || otherDataDocXPathNav.Document != _doc ?
                 XmlNodeOrder.Unknown :
index 768834a..d04cd7d 100644 (file)
@@ -1,9 +1,6 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-// TODO: Enable after System.Private.Xml is annotated
-#nullable disable
-
 using System.Data;
 using System.Diagnostics;
 
@@ -15,7 +12,7 @@ namespace System.Xml
     {
         private XmlDataDocument _doc;
         private XmlNode _node;
-        private DataColumn _column;
+        private DataColumn? _column;
         private bool _fOnValue;
         private bool _bNeedFoliate;
         private bool _isInUse;
@@ -45,9 +42,9 @@ namespace System.Xml
         internal void AddPointer() => _doc.AddPointer(this);
 
         // Returns the row element of the region that the pointer points into
-        private XmlBoundElement GetRowElement()
+        private XmlBoundElement? GetRowElement()
         {
-            XmlBoundElement rowElem;
+            XmlBoundElement? rowElem;
             if (_column != null)
             {
                 rowElem = _node as XmlBoundElement;
@@ -60,11 +57,11 @@ namespace System.Xml
             return rowElem;
         }
 
-        private DataRow Row
+        private DataRow? Row
         {
             get
             {
-                XmlBoundElement rowElem = GetRowElement();
+                XmlBoundElement? rowElem = GetRowElement();
                 if (rowElem == null)
                 {
                     return null;
@@ -103,7 +100,7 @@ namespace System.Xml
             AssertValid();
         }
 
-        private void MoveTo(XmlNode node, DataColumn column, bool fOnValue)
+        private void MoveTo(XmlNode node, DataColumn? column, bool fOnValue)
         {
             // You should not move outside of this document
             Debug.Assert(node == _doc || node.OwnerDocument == _doc);
@@ -114,7 +111,7 @@ namespace System.Xml
             AssertValid();
         }
 
-        private DataColumn NextColumn(DataRow row, DataColumn col, bool fAttribute, bool fNulls)
+        private DataColumn? NextColumn(DataRow row, DataColumn? col, bool fAttribute, bool fNulls)
         {
             if (row.RowState == DataRowState.Deleted)
             {
@@ -139,9 +136,9 @@ namespace System.Xml
             return null;
         }
 
-        private DataColumn NthColumn(DataRow row, bool fAttribute, int iColumn, bool fNulls)
+        private DataColumn? NthColumn(DataRow row, bool fAttribute, int iColumn, bool fNulls)
         {
-            DataColumn c = null;
+            DataColumn? c = null;
             while ((c = NextColumn(row, c, fAttribute, fNulls)) != null)
             {
                 if (iColumn == 0)
@@ -156,7 +153,7 @@ namespace System.Xml
 
         private int ColumnCount(DataRow row, bool fAttribute, bool fNulls)
         {
-            DataColumn c = null;
+            DataColumn? c = null;
             int count = 0;
             while ((c = NextColumn(row, c, fAttribute, fNulls)) != null)
             {
@@ -187,7 +184,7 @@ namespace System.Xml
             else if (!IsFoliated(_node))
             {
                 // find virtual column elements first
-                DataColumn c = NextColumn(Row, null, false, false);
+                DataColumn? c = NextColumn(Row!, null, false, false);
                 if (c != null)
                 {
                     MoveTo(_node, c, _doc.IsTextOnly(c));
@@ -196,7 +193,7 @@ namespace System.Xml
             }
 
             // look for anything
-            XmlNode n = _doc.SafeFirstChild(_node);
+            XmlNode? n = _doc.SafeFirstChild(_node);
             if (n != null)
             {
                 MoveTo(n);
@@ -219,14 +216,14 @@ namespace System.Xml
                         return false;
                     }
 
-                    DataColumn c = NextColumn(Row, _column, false, false);
+                    DataColumn? c = NextColumn(Row!, _column, false, false);
                     if (c != null)
                     {
                         MoveTo(_node, c, false);
                         return true;
                     }
 
-                    XmlNode n = _doc.SafeFirstChild(_node);
+                    XmlNode? n = _doc.SafeFirstChild(_node);
                     if (n != null)
                     {
                         MoveTo(n);
@@ -235,7 +232,7 @@ namespace System.Xml
                 }
                 else
                 {
-                    XmlNode n = _doc.SafeNextSibling(_node);
+                    XmlNode? n = _doc.SafeNextSibling(_node);
                     if (n != null)
                     {
                         MoveTo(n);
@@ -269,7 +266,7 @@ namespace System.Xml
                 }
                 else
                 {
-                    XmlNode n = _node.ParentNode;
+                    XmlNode? n = _node.ParentNode;
                     if (n != null)
                     {
                         MoveTo(n);
@@ -298,7 +295,7 @@ namespace System.Xml
                 }
                 else if (_node.NodeType == XmlNodeType.Attribute)
                 {
-                    XmlNode n = ((XmlAttribute)_node).OwnerElement;
+                    XmlNode? n = ((XmlAttribute)_node).OwnerElement;
                     if (n != null)
                     {
                         MoveTo(n, null, false);
@@ -323,11 +320,11 @@ namespace System.Xml
                     {
                         if (!IsFoliated(_node))
                         {
-                            return ColumnCount(Row, true, false);
+                            return ColumnCount(Row!, true, false);
                         }
                         else
                         {
-                            return _node.Attributes.Count;
+                            return _node.Attributes!.Count;
                         }
                     }
                 }
@@ -350,7 +347,7 @@ namespace System.Xml
                 {
                     if (!IsFoliated(_node))
                     {
-                        DataColumn c = NthColumn(Row, true, i, false);
+                        DataColumn? c = NthColumn(Row!, true, i, false);
                         if (c != null)
                         {
                             MoveTo(_node, c, false);
@@ -359,7 +356,7 @@ namespace System.Xml
                     }
                     else
                     {
-                        XmlNode n = _node.Attributes.Item(i);
+                        XmlNode? n = _node.Attributes!.Item(i);
                         if (n != null)
                         {
                             MoveTo(n, null, false);
@@ -551,7 +548,7 @@ namespace System.Xml
             }
         }
 
-        internal string Value
+        internal string? Value
         {
             get
             {
@@ -567,7 +564,7 @@ namespace System.Xml
                 }
                 else if (_column.ColumnMapping == MappingType.Attribute || _fOnValue)
                 {
-                    DataRow row = Row;
+                    DataRow row = Row!;
                     DataRowVersion rowVersion = (row.RowState == DataRowState.Detached) ? DataRowVersion.Proposed : DataRowVersion.Current;
                     object value = row[_column, rowVersion];
                     if (!Convert.IsDBNull(value))
@@ -590,7 +587,7 @@ namespace System.Xml
             return nodeToCheck == _node;
         }
 
-        bool IXmlDataVirtualNode.IsOnColumn(DataColumn col)
+        bool IXmlDataVirtualNode.IsOnColumn(DataColumn? col)
         {
             RealFoliate();
             return col == _column;
@@ -651,17 +648,17 @@ namespace System.Xml
                 return;
             }
 
-            XmlNode n = null;
+            XmlNode? n = null;
 
-            if (_doc.IsTextOnly(_column))
+            if (_doc.IsTextOnly(_column!))
             {
                 n = _node.FirstChild;
             }
             else
             {
-                if (_column.ColumnMapping == MappingType.Attribute)
+                if (_column!.ColumnMapping == MappingType.Attribute)
                 {
-                    n = _node.Attributes.GetNamedItem(_column.EncodedColumnName, _column.Namespace);
+                    n = _node.Attributes!.GetNamedItem(_column.EncodedColumnName, _column.Namespace);
                 }
                 else
                 {
@@ -693,7 +690,7 @@ namespace System.Xml
         }
 
         //for the 6 properties below, only when the this.column == null that the nodetype could be XmlDeclaration node
-        internal string PublicId
+        internal string? PublicId
         {
             get
             {
@@ -720,7 +717,7 @@ namespace System.Xml
             }
         }
 
-        internal string SystemId
+        internal string? SystemId
         {
             get
             {
@@ -747,7 +744,7 @@ namespace System.Xml
             }
         }
 
-        internal string InternalSubset
+        internal string? InternalSubset
         {
             get
             {
@@ -760,18 +757,18 @@ namespace System.Xml
             }
         }
 
-        internal XmlDeclaration Declaration
+        internal XmlDeclaration? Declaration
         {
             get
             {
-                XmlNode child = _doc.SafeFirstChild(_doc);
+                XmlNode? child = _doc.SafeFirstChild(_doc);
                 if (child != null && child.NodeType == XmlNodeType.XmlDeclaration)
                     return (XmlDeclaration)child;
                 return null;
             }
         }
 
-        internal string Encoding
+        internal string? Encoding
         {
             get
             {
@@ -782,7 +779,7 @@ namespace System.Xml
                 }
                 else if (NodeType == XmlNodeType.Document)
                 {
-                    XmlDeclaration dec = Declaration;
+                    XmlDeclaration? dec = Declaration;
                     if (dec != null)
                     {
                         return dec.Encoding;
@@ -792,7 +789,7 @@ namespace System.Xml
             }
         }
 
-        internal string Standalone
+        internal string? Standalone
         {
             get
             {
@@ -803,7 +800,7 @@ namespace System.Xml
                 }
                 else if (NodeType == XmlNodeType.Document)
                 {
-                    XmlDeclaration dec = Declaration;
+                    XmlDeclaration? dec = Declaration;
                     if (dec != null)
                     {
                         return dec.Standalone;
@@ -813,7 +810,7 @@ namespace System.Xml
             }
         }
 
-        internal string Version
+        internal string? Version
         {
             get
             {
@@ -824,7 +821,7 @@ namespace System.Xml
                 }
                 else if (NodeType == XmlNodeType.Document)
                 {
-                    XmlDeclaration dec = Declaration;
+                    XmlDeclaration? dec = Declaration;
                     if (dec != null)
                         return dec.Version;
                 }
@@ -839,10 +836,10 @@ namespace System.Xml
             if (_column != null)
             {
                 // We must be on a de-foliated region
-                XmlBoundElement rowElem = _node as XmlBoundElement;
+                XmlBoundElement? rowElem = _node as XmlBoundElement;
                 Debug.Assert(rowElem != null);
 
-                DataRow row = rowElem.Row;
+                DataRow? row = rowElem.Row;
                 Debug.Assert(row != null);
 
                 ElementState state = rowElem.ElementState;
@@ -861,7 +858,7 @@ namespace System.Xml
 
         internal void SetNoLongerUse()
         {
-            _node = null;
+            _node = null!;
             _column = null;
             _fOnValue = false;
             _bNeedFoliate = false;
index 95a2e50..5548019 100644 (file)
@@ -1,12 +1,10 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-// TODO: Enable after System.Private.Xml is annotated
-#nullable disable
-
 using System.Collections;
 using System.Data;
 using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
 
 #pragma warning disable 0618 // ignore obsolete warning about XmlDataDocument
 
@@ -25,8 +23,8 @@ namespace System.Xml
         private Hashtable _tableSchemaMap;   // maps an string (currently this is localName:nsURI) to a DataTable. Used to quickly find if a bound-elem matches any data-table metadata..
         private Hashtable _columnSchemaMap;  // maps a string (table localName:nsURI) to a Hashtable. The 2nd hastable (the one that is stored as data in columnSchemaMap, maps a string to a DataColumn.
 
-        private XmlDataDocument _doc;        // The document this mapper is related to
-        private DataSet _dataSet;          // The dataset this mapper is related to
+        private XmlDataDocument? _doc;        // The document this mapper is related to
+        private DataSet? _dataSet;          // The dataset this mapper is related to
         internal const string strReservedXmlns = "http://www.w3.org/2000/xmlns/";
 
         internal DataSetMapper()
@@ -66,7 +64,7 @@ namespace System.Xml
         internal DataTable SearchMatchingTableSchema(string localName, string namespaceURI)
         {
             object tid = GetIdentity(localName, namespaceURI);
-            return (DataTable)(_tableSchemaMap[tid]);
+            return (DataTable)(_tableSchemaMap[tid]!);
         }
 
         // SearchMatchingTableSchema function works only when the elem has not been bound to a DataRow. If you want to get the table associated w/ an element after
@@ -86,7 +84,7 @@ namespace System.Xml
         //                  a column from the parent region table, then the node is NOT associated w/ a DataTable (it is a potential DataColumn in the parent region)
         //              3.3.2 Else the node is a row-element (and associated w/ a DataTable / DataRow )
         //
-        internal DataTable SearchMatchingTableSchema(XmlBoundElement rowElem, XmlBoundElement elem)
+        internal DataTable? SearchMatchingTableSchema(XmlBoundElement? rowElem, XmlBoundElement elem)
         {
             Debug.Assert(elem != null);
 
@@ -104,7 +102,7 @@ namespace System.Xml
             // Currently we expect we map things from top of the tree to the bottom
             Debug.Assert(rowElem.Row != null);
 
-            DataColumn col = GetColumnSchemaForNode(rowElem, elem);
+            DataColumn? col = GetColumnSchemaForNode(rowElem, elem);
             if (col == null)
             {
                 return t;
@@ -135,7 +133,7 @@ namespace System.Xml
                 }
             }
 
-            for (XmlNode n = elem.FirstChild; n != null; n = n.NextSibling)
+            for (XmlNode? n = elem.FirstChild; n != null; n = n.NextSibling)
             {
                 if (n.NodeType == XmlNodeType.Element)
                 {
@@ -148,7 +146,7 @@ namespace System.Xml
             return null;
         }
 
-        internal DataColumn GetColumnSchemaForNode(XmlBoundElement rowElem, XmlNode node)
+        internal DataColumn? GetColumnSchemaForNode(XmlBoundElement rowElem, XmlNode node)
         {
             Debug.Assert(rowElem != null);
             // The caller must make sure that node is not a row-element
@@ -157,10 +155,10 @@ namespace System.Xml
             object tid = GetIdentity(rowElem.LocalName, rowElem.NamespaceURI);
             object cid = GetIdentity(node.LocalName, node.NamespaceURI);
 
-            Hashtable columns = (Hashtable)_columnSchemaMap[tid];
+            Hashtable? columns = (Hashtable?)_columnSchemaMap[tid];
             if (columns != null)
             {
-                DataColumn col = (DataColumn)(columns[cid]);
+                DataColumn? col = (DataColumn?)(columns[cid]);
                 if (col == null)
                 {
                     return null;
@@ -183,9 +181,9 @@ namespace System.Xml
             }
             return null;
         }
-        internal DataTable GetTableSchemaForElement(XmlElement elem)
+        internal DataTable? GetTableSchemaForElement(XmlElement elem)
         {
-            XmlBoundElement be = elem as XmlBoundElement;
+            XmlBoundElement? be = elem as XmlBoundElement;
             if (be == null)
             {
                 return null;
@@ -194,7 +192,7 @@ namespace System.Xml
             return GetTableSchemaForElement(be);
         }
 
-        internal DataTable GetTableSchemaForElement(XmlBoundElement be) => be.Row?.Table;
+        internal DataTable? GetTableSchemaForElement(XmlBoundElement be) => be.Row?.Table;
 
         internal static bool IsNotMapped(DataColumn c) => c.ColumnMapping == MappingType.Hidden;
 
@@ -204,18 +202,18 @@ namespace System.Xml
         //     XmlElement e = be;
         //     GetRowFromElement( be ); // Calls GetRowFromElement( XmlBoundElement )
         //     GetRowFromElement( e );  // Calls GetRowFromElement( XmlElement ), in spite of e beeing an instance of XmlBoundElement
-        internal DataRow GetRowFromElement(XmlElement e) => (e as XmlBoundElement)?.Row;
+        internal DataRow? GetRowFromElement(XmlElement? e) => (e as XmlBoundElement)?.Row;
 
-        internal DataRow GetRowFromElement(XmlBoundElement be) => be.Row;
+        internal DataRow? GetRowFromElement(XmlBoundElement be) => be.Row;
 
         // Get the row-elem associatd w/ the region node is in.
         // If node is in a region not mapped (like document element node) the function returns false and sets elem to null)
         // This function does not work if the region is not associated w/ a DataRow (it uses DataRow association to know what is the row element associated w/ the region)
-        internal bool GetRegion(XmlNode node, out XmlBoundElement rowElem)
+        internal bool GetRegion(XmlNode? node, [NotNullWhen(true)] out XmlBoundElement? rowElem)
         {
             while (node != null)
             {
-                XmlBoundElement be = node as XmlBoundElement;
+                XmlBoundElement? be = node as XmlBoundElement;
                 // Break if found a region
                 if (be != null && GetRowFromElement(be) != null)
                 {
@@ -247,7 +245,7 @@ namespace System.Xml
                 return true;
             }
 
-            DataTable table = GetTableSchemaForElement(rowElem);
+            DataTable table = GetTableSchemaForElement(rowElem)!;
             DataColumnCollection columns = table.Columns;
             int iColumn = 0;
 
@@ -264,7 +262,7 @@ namespace System.Xml
                 }
 
                 // only mapped attrs are valid
-                DataColumn schema = GetColumnSchemaForNode(rowElem, attr);
+                DataColumn? schema = GetColumnSchemaForNode(rowElem, attr);
                 if (schema == null)
                 {
                     return false;
@@ -277,7 +275,7 @@ namespace System.Xml
                 }
 
                 // must have exactly one text node (XmlNodeType.Text) child
-                XmlNode fc = attr.FirstChild;
+                XmlNode? fc = attr.FirstChild;
                 if (fc == null || fc.NodeType != XmlNodeType.Text || fc.NextSibling != null)
                 {
                     return false;
@@ -286,7 +284,7 @@ namespace System.Xml
 
             // check column elements
             iColumn = 0;
-            XmlNode n = rowElem.FirstChild;
+            XmlNode? n = rowElem.FirstChild;
             for (; n != null; n = n.NextSibling)
             {
                 // only elements can exist in radically structured data
@@ -294,7 +292,7 @@ namespace System.Xml
                 {
                     return false;
                 }
-                XmlElement e = n as XmlElement;
+                XmlElement? e = n as XmlElement;
 
                 // only checking for column mappings in this loop
                 if (GetRowFromElement(e) != null)
@@ -303,7 +301,7 @@ namespace System.Xml
                 }
 
                 // element's must have schema to be radically structured
-                DataColumn schema = GetColumnSchemaForNode(rowElem, e);
+                DataColumn? schema = GetColumnSchemaForNode(rowElem, e!);
                 if (schema == null)
                 {
                     return false;
@@ -322,7 +320,7 @@ namespace System.Xml
                 }
 
                 // must have exactly one text node child
-                XmlNode fc = e.FirstChild;
+                XmlNode? fc = e.FirstChild;
                 if (fc == null || fc.NodeType != XmlNodeType.Text || fc.NextSibling != null)
                 {
                     return false;
@@ -339,7 +337,7 @@ namespace System.Xml
                 }
 
                 // element's must be regions in order to be radially structured
-                DataRow row = GetRowFromElement((XmlElement)n);
+                DataRow? row = GetRowFromElement((XmlElement?)n);
                 if (row == null)
                 {
                     return false;
@@ -356,11 +354,11 @@ namespace System.Xml
         }
         private void AddColumnSchema(DataColumn col)
         {
-            DataTable table = col.Table;
+            DataTable table = col.Table!;
             object idTable = GetIdentity(table.EncodedTableName, table.Namespace);
             object idColumn = GetIdentity(col.EncodedColumnName, col.Namespace);
 
-            Hashtable columns = (Hashtable)_columnSchemaMap[idTable];
+            Hashtable? columns = (Hashtable?)_columnSchemaMap[idTable];
             if (columns == null)
             {
                 columns = new Hashtable();
index 18dbd1e..81052d7 100644 (file)
@@ -1,9 +1,6 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-// TODO: Enable after System.Private.Xml is annotated
-#nullable disable
-
 using System.Data;
 
 namespace System.Xml
@@ -11,7 +8,7 @@ namespace System.Xml
     internal interface IXmlDataVirtualNode
     {
         bool IsOnNode(XmlNode nodeToCheck);
-        bool IsOnColumn(DataColumn col);
+        bool IsOnColumn(DataColumn? col);
         bool IsInUse();
         void OnFoliated(XmlNode foliatedNode);
     }
index 5be8426..1a6545e 100644 (file)
@@ -1,10 +1,8 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-// TODO: Enable after System.Private.Xml is annotated
-#nullable disable
-
 using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
 using System.Text;
 
 #pragma warning disable 618 // ignore obsolete warning about XmlDataDocument
@@ -29,11 +27,12 @@ namespace System.Xml
             _currentNode = rowElement;
         }
 
-        internal override XmlNode CurrentNode => _currentNode;
+        internal override XmlNode? CurrentNode => _currentNode;
 
+        [MemberNotNullWhen(true, nameof(CurrentNode))]
         internal override bool Next()
         {
-            XmlNode nextNode;
+            XmlNode? nextNode;
             ElementState oldState = _rowElement.ElementState;
 
             // We do not want to cause any foliation w/ this iterator or use this iterator once the region was defoliated
@@ -46,6 +45,7 @@ namespace System.Xml
             if (nextNode != null)
             {
                 _currentNode = nextNode;
+                Debug.Assert(CurrentNode != null);
                 // If we have been defoliated, we should have stayed that way
                 Debug.Assert((oldState == ElementState.Defoliated) ? (_rowElement.ElementState == ElementState.Defoliated) : true);
                 // Rollback foliation
@@ -56,12 +56,13 @@ namespace System.Xml
             return NextRight();
         }
 
+        [MemberNotNullWhen(true, nameof(CurrentNode))]
         internal override bool NextRight()
         {
             // Make sure we do not get past the rowElement if we call NextRight on a just initialized iterator and rowElement has no children
             if (_currentNode == _rowElement)
             {
-                _currentNode = null;
+                _currentNode = null!;
                 return false;
             }
 
@@ -70,7 +71,7 @@ namespace System.Xml
             // We do not want to cause any foliation w/ this iterator or use this iterator once the region was defoliated
             Debug.Assert(oldState != ElementState.None);
 
-            XmlNode nextNode = _currentNode.NextSibling;
+            XmlNode? nextNode = _currentNode.NextSibling;
 
             if (nextNode != null)
             {
@@ -79,19 +80,20 @@ namespace System.Xml
                 Debug.Assert((oldState == ElementState.Defoliated) ? (_rowElement.ElementState == ElementState.Defoliated) : true);
                 // Rollback foliation
                 _rowElement.ElementState = oldState;
+                Debug.Assert(CurrentNode != null);
                 return true;
             }
 
             // No next sibling, try the first sibling of from the parent chain
             nextNode = _currentNode;
-            while (nextNode != _rowElement && nextNode.NextSibling == null)
+            while (nextNode != _rowElement && nextNode!.NextSibling == null)
             {
                 nextNode = nextNode.ParentNode;
             }
 
             if (nextNode == _rowElement)
             {
-                _currentNode = null;
+                _currentNode = null!;
                 // If we have been defoliated, we should have stayed that way
                 Debug.Assert((oldState == ElementState.Defoliated) ? (_rowElement.ElementState == ElementState.Defoliated) : true);
 
@@ -100,19 +102,21 @@ namespace System.Xml
                 return false;
             }
 
+            Debug.Assert(nextNode.NextSibling != null);
             _currentNode = nextNode.NextSibling;
-            Debug.Assert(_currentNode != null);
 
             // If we have been defoliated, we should have stayed that way
             Debug.Assert((oldState == ElementState.Defoliated) ? (_rowElement.ElementState == ElementState.Defoliated) : true);
 
             // Rollback foliation
             _rowElement.ElementState = oldState;
+            Debug.Assert(CurrentNode != null);
             return true;
         }
 
         // Get the initial text value for the current node. You should be positioned on the node (element) for
         // which to get the initial text value, not on the text node.
+        [MemberNotNullWhen(true, nameof(CurrentNode))]
         internal bool NextInitialTextLikeNodes(out string value)
         {
             Debug.Assert(CurrentNode != null);
@@ -133,7 +137,7 @@ namespace System.Xml
             // We do not want to cause any foliation w/ this iterator or use this iterator once the region was defoliated
             Debug.Assert(oldState != ElementState.None);
 
-            XmlNode n = CurrentNode.FirstChild;
+            XmlNode? n = CurrentNode.FirstChild;
             value = GetInitialTextFromNodes(ref n);
             if (n == null)
             {
@@ -155,9 +159,9 @@ namespace System.Xml
             return true;
         }
 
-        private static string GetInitialTextFromNodes(ref XmlNode n)
+        private static string GetInitialTextFromNodes(ref XmlNode? n)
         {
-            string value = null;
+            string? value = null;
 
             if (n != null)
             {
index 26bc84f..2361b77 100644 (file)
@@ -1,9 +1,6 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-// TODO: Enable after System.Private.Xml is annotated
-#nullable disable
-
 using System.Diagnostics;
 
 #pragma warning disable 0618 // ignore obsolete warning about XmlDataDocument
@@ -16,18 +13,18 @@ namespace System.Xml
         private readonly XmlNode _nodeTop;
         private XmlNode _currentNode;
 
-        internal TreeIterator(XmlNode nodeTop) : base(((XmlDataDocument)(nodeTop.OwnerDocument)).Mapper)
+        internal TreeIterator(XmlNode nodeTop) : base(((XmlDataDocument)(nodeTop.OwnerDocument!)).Mapper)
         {
             Debug.Assert(nodeTop != null);
             _nodeTop = nodeTop;
             _currentNode = nodeTop;
         }
 
-        internal override XmlNode CurrentNode => _currentNode;
+        internal override XmlNode? CurrentNode => _currentNode;
 
         internal override bool Next()
         {
-            XmlNode nextNode;
+            XmlNode? nextNode;
 
             // Try to move to the first child
             nextNode = _currentNode.FirstChild;
@@ -47,11 +44,11 @@ namespace System.Xml
             // Make sure we do not get past the nodeTop if we call NextRight on a just initialized iterator and nodeTop has no children
             if (_currentNode == _nodeTop)
             {
-                _currentNode = null;
+                _currentNode = null!;
                 return false;
             }
 
-            XmlNode nextNode = _currentNode.NextSibling;
+            XmlNode? nextNode = _currentNode.NextSibling;
 
             if (nextNode != null)
             {
@@ -61,19 +58,19 @@ namespace System.Xml
 
             // No next sibling, try the first sibling of from the parent chain
             nextNode = _currentNode;
-            while (nextNode != _nodeTop && nextNode.NextSibling == null)
+            while (nextNode != _nodeTop && nextNode!.NextSibling == null)
             {
                 nextNode = nextNode.ParentNode;
             }
 
             if (nextNode == _nodeTop)
             {
-                _currentNode = null;
+                _currentNode = null!;
                 return false;
             }
 
+            Debug.Assert(nextNode.NextSibling != null);
             _currentNode = nextNode.NextSibling;
-            Debug.Assert(_currentNode != null);
             return true;
         }
     }
index d313230..f709582 100644 (file)
@@ -1,9 +1,6 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-// TODO: Enable after System.Private.Xml is annotated
-#nullable disable
-
 using System.Data;
 using System.Diagnostics;
 using System.Xml.XPath;
@@ -17,9 +14,9 @@ namespace System.Xml
         private readonly WeakReference _owner;  // Owner of this pointer (an DataDocumentXPathNavigator). When the associated DataDocumentXPathNavigator (the owner) goes away, this XPathNodePointer can go away as well.
         private readonly XmlDataDocument _doc;
         private XmlNode _node;
-        private DataColumn _column;
+        private DataColumn? _column;
         private bool _fOnValue;
-        internal XmlBoundElement _parentOfNS;
+        internal XmlBoundElement? _parentOfNS;
         internal static readonly int[] s_xmlNodeType_To_XpathNodeType_Map = CreateXmlNodeTypeToXpathNodeTypeMap();
         internal const string StrReservedXmlns = "http://www.w3.org/2000/xmlns/";
         internal const string StrReservedXml = "http://www.w3.org/XML/1998/namespace";
@@ -33,7 +30,7 @@ namespace System.Xml
             Array enumValues = Enum.GetValues(typeof(XmlNodeType));
             for (int i = 0; i < enumValues.Length; i++)
             {
-                tempVal = (int)enumValues.GetValue(i);
+                tempVal = (int)enumValues.GetValue(i)!;
                 if (tempVal > max)
                     max = tempVal;
             }
@@ -67,9 +64,10 @@ namespace System.Xml
             //the function can only be called on text like nodes.
             Debug.Assert(XmlDataDocument.IsTextNode(node.NodeType));
             XPathNodeType xnt = XPathNodeType.Whitespace;
-            while (node != null)
+            XmlNode? n = node;
+            while (n != null)
             {
-                switch (node.NodeType)
+                switch (n.NodeType)
                 {
                     case XmlNodeType.Whitespace:
                         break;
@@ -82,7 +80,7 @@ namespace System.Xml
                     default:
                         return xnt;
                 }
-                node = _doc.SafeNextSibling(node);
+                n = _doc.SafeNextSibling(n);
             }
             return xnt;
         }
@@ -118,7 +116,7 @@ namespace System.Xml
         {
         }
 
-        private XPathNodePointer(DataDocumentXPathNavigator owner, XmlDataDocument doc, XmlNode node, DataColumn c, bool bOnValue, XmlBoundElement parentOfNS)
+        private XPathNodePointer(DataDocumentXPathNavigator owner, XmlDataDocument doc, XmlNode node, DataColumn? c, bool bOnValue, XmlBoundElement? parentOfNS)
         {
             Debug.Assert(owner != null);
             _owner = new WeakReference(owner);
@@ -311,7 +309,7 @@ namespace System.Xml
             }
         }
 
-        internal string Value
+        internal string? Value
         {
             get
             {
@@ -321,14 +319,14 @@ namespace System.Xml
                     return null;
                 else if (_column == null)
                 {
-                    string strRet = _node.Value;
+                    string? strRet = _node.Value;
                     if (XmlDataDocument.IsTextNode(_node.NodeType))
                     {
                         //concatenate adjacent textlike nodes
-                        XmlNode parent = _node.ParentNode;
+                        XmlNode? parent = _node.ParentNode;
                         if (parent == null)
                             return strRet;
-                        XmlNode n = _doc.SafeNextSibling(_node);
+                        XmlNode? n = _doc.SafeNextSibling(_node);
                         while (n != null && XmlDataDocument.IsTextNode(n.NodeType))
                         {
                             strRet += n.Value;
@@ -339,7 +337,7 @@ namespace System.Xml
                 }
                 else if (_column.ColumnMapping == MappingType.Attribute || _fOnValue)
                 {
-                    DataRow row = Row;
+                    DataRow row = Row!;
                     DataRowVersion rowVersion = (row.RowState == DataRowState.Detached) ? DataRowVersion.Proposed : DataRowVersion.Current;
                     object value = row[_column, rowVersion];
                     if (!Convert.IsDBNull(value))
@@ -366,7 +364,7 @@ namespace System.Xml
                     if (_node.NodeType == XmlNodeType.Document)
                     {
                         //document node's region should always be uncompressed
-                        XmlElement rootElem = ((XmlDocument)_node).DocumentElement;
+                        XmlElement? rootElem = ((XmlDocument)_node).DocumentElement;
                         if (rootElem != null)
                             return rootElem.InnerText;
                         return string.Empty;
@@ -376,7 +374,7 @@ namespace System.Xml
                 }
                 else
                 {
-                    DataRow row = Row;
+                    DataRow row = Row!;
                     DataRowVersion rowVersion = (row.RowState == DataRowState.Detached) ? DataRowVersion.Proposed : DataRowVersion.Current;
                     object value = row[_column, rowVersion];
                     if (!Convert.IsDBNull(value))
@@ -403,8 +401,8 @@ namespace System.Xml
             {
                 RealFoliate();
                 XmlNode curNode = _node;
-                XmlBoundElement curBoundElem = null;
-                object colVal = null;
+                XmlBoundElement? curBoundElem = null;
+                object? colVal = null;
                 while (curNode != null)
                 {
                     curBoundElem = curNode as XmlBoundElement;
@@ -413,7 +411,7 @@ namespace System.Xml
                         if (curBoundElem.ElementState == ElementState.Defoliated)
                         {
                             //if not foliated, going through the columns to get the xml:lang
-                            DataRow row = curBoundElem.Row;
+                            DataRow row = curBoundElem.Row!;
                             foreach (DataColumn col in row.Table.Columns)
                             {
                                 if (col.Prefix == "xml" && col.EncodedColumnName == "lang")
@@ -433,20 +431,20 @@ namespace System.Xml
                         }
                     }
                     if (curNode.NodeType == XmlNodeType.Attribute)
-                        curNode = ((XmlAttribute)curNode).OwnerElement;
+                        curNode = ((XmlAttribute)curNode).OwnerElement!;
                     else
-                        curNode = curNode.ParentNode;
+                        curNode = curNode.ParentNode!;
                 }
                 return string.Empty;
             }
         }
 
-        private XmlBoundElement GetRowElement()
+        private XmlBoundElement? GetRowElement()
         {
-            XmlBoundElement rowElem;
+            XmlBoundElement? rowElem;
             if (_column != null)
             {
-                rowElem = _node as XmlBoundElement;
+                rowElem = (XmlBoundElement)_node;
                 Debug.Assert(rowElem != null);
                 Debug.Assert(rowElem.Row != null);
                 return rowElem;
@@ -456,11 +454,11 @@ namespace System.Xml
             return rowElem;
         }
 
-        private DataRow Row
+        private DataRow? Row
         {
             get
             {
-                XmlBoundElement rowElem = GetRowElement();
+                XmlBoundElement? rowElem = GetRowElement();
                 if (rowElem == null)
                     return null;
 
@@ -494,7 +492,7 @@ namespace System.Xml
             _fOnValue = false;
         }
 
-        private void MoveTo(XmlNode node, DataColumn column, bool fOnValue)
+        private void MoveTo(XmlNode node, DataColumn? column, bool fOnValue)
         {
             // Should not move outside of this document
             Debug.Assert(node == _doc || node.OwnerDocument == _doc);
@@ -512,7 +510,7 @@ namespace System.Xml
 
         private int ColumnCount(DataRow row, bool fAttribute)
         {
-            DataColumn c = null;
+            DataColumn? c = null;
             int count = 0;
             while ((c = NextColumn(row, c, fAttribute)) != null)
             {
@@ -533,11 +531,11 @@ namespace System.Xml
                     if (_column == null && _node.NodeType == XmlNodeType.Element)
                     {
                         if (!IsFoliated(_node))
-                            return ColumnCount(Row, true);
+                            return ColumnCount(Row!, true);
                         else
                         {
                             int nc = 0;
-                            foreach (XmlAttribute attr in _node.Attributes)
+                            foreach (XmlAttribute attr in _node.Attributes!)
                             {
                                 if (attr.NamespaceURI != StrReservedXmlns)
                                     nc++;
@@ -550,7 +548,7 @@ namespace System.Xml
             }
         }
 
-        internal DataColumn NextColumn(DataRow row, DataColumn col, bool fAttribute)
+        internal DataColumn? NextColumn(DataRow row, DataColumn? col, bool fAttribute)
         {
             if (row.RowState == DataRowState.Deleted)
                 return null;
@@ -571,7 +569,7 @@ namespace System.Xml
             return null;
         }
 
-        internal DataColumn PreviousColumn(DataRow row, DataColumn col, bool fAttribute)
+        internal DataColumn? PreviousColumn(DataRow row, DataColumn? col, bool fAttribute)
         {
             if (row.RowState == DataRowState.Deleted)
                 return null;
@@ -605,8 +603,8 @@ namespace System.Xml
                 {
                     if (!IsFoliated(_node))
                     {
-                        DataColumn c = null;
-                        while ((c = NextColumn(Row, c, true)) != null)
+                        DataColumn? c = null;
+                        while ((c = NextColumn(Row!, c, true)) != null)
                         {
                             if (c.EncodedColumnName == localName && c.Namespace == namespaceURI)
                             {
@@ -618,7 +616,7 @@ namespace System.Xml
                     else
                     {
                         Debug.Assert(_node.Attributes != null);
-                        XmlNode n = _node.Attributes.GetNamedItem(localName, namespaceURI);
+                        XmlNode? n = _node.Attributes.GetNamedItem(localName, namespaceURI);
                         if (n != null)
                         {
                             MoveTo(n, null, false);
@@ -647,8 +645,8 @@ namespace System.Xml
                 }
                 if (!IsFoliated(_node))
                 {
-                    DataColumn c = _column;
-                    while ((c = NextColumn(Row, c, true)) != null)
+                    DataColumn? c = _column;
+                    while ((c = NextColumn(Row!, c, true)) != null)
                     {
                         if (c.Namespace != StrReservedXmlns)
                         {
@@ -662,7 +660,7 @@ namespace System.Xml
                 {
                     if (bFirst)
                     {
-                        XmlAttributeCollection attrs = _node.Attributes;
+                        XmlAttributeCollection attrs = _node.Attributes!;
                         foreach (XmlAttribute attr in attrs)
                         {
                             if (attr.NamespaceURI != StrReservedXmlns)
@@ -674,7 +672,7 @@ namespace System.Xml
                     }
                     else
                     {
-                        XmlAttributeCollection attrs = ((XmlAttribute)_node).OwnerElement.Attributes;
+                        XmlAttributeCollection attrs = ((XmlAttribute)_node).OwnerElement!.Attributes;
                         bool bFound = false;
                         foreach (XmlAttribute attr in attrs)
                         {
@@ -744,8 +742,8 @@ namespace System.Xml
                         Debug.Assert(_column.ColumnMapping != MappingType.Attribute && _column.ColumnMapping != MappingType.Hidden);
                         return false;
                     }
-                    DataRow curRow = Row;
-                    DataColumn c = NextColumn(curRow, _column, false);
+                    DataRow curRow = Row!;
+                    DataColumn? c = NextColumn(curRow, _column, false);
                     while (c != null)
                     {
                         if (IsValidChild(_node, c))
@@ -755,7 +753,7 @@ namespace System.Xml
                         }
                         c = NextColumn(curRow, c, false);
                     }
-                    XmlNode n = _doc.SafeFirstChild(_node);
+                    XmlNode? n = _doc.SafeFirstChild(_node);
                     if (n != null)
                     {
                         MoveTo(n);
@@ -764,8 +762,8 @@ namespace System.Xml
                 }
                 else
                 {
-                    XmlNode n = _node;
-                    XmlNode parent = _node.ParentNode;
+                    XmlNode? n = _node;
+                    XmlNode? parent = _node.ParentNode;
                     if (parent == null)
                         return false;
                     bool bTextLike = XmlDataDocument.IsTextNode(_node.NodeType);
@@ -796,8 +794,8 @@ namespace System.Xml
                 {
                     if (_fOnValue)
                         return false;
-                    DataRow curRow = Row;
-                    DataColumn c = PreviousColumn(curRow, _column, false);
+                    DataRow curRow = Row!;
+                    DataColumn? c = PreviousColumn(curRow, _column, false);
                     while (c != null)
                     {
                         if (IsValidChild(_node, c))
@@ -810,8 +808,8 @@ namespace System.Xml
                 }
                 else
                 {
-                    XmlNode n = _node;
-                    XmlNode parent = _node.ParentNode;
+                    XmlNode? n = _node;
+                    XmlNode? parent = _node.ParentNode;
                     if (parent == null)
                         return false;
                     bool bTextLike = XmlDataDocument.IsTextNode(_node.NodeType);
@@ -829,10 +827,10 @@ namespace System.Xml
                     }
                     if (!IsFoliated(parent) && (parent is XmlBoundElement))
                     {
-                        DataRow row = ((XmlBoundElement)parent).Row;
+                        DataRow? row = ((XmlBoundElement)parent).Row;
                         if (row != null)
                         {
-                            DataColumn c = PreviousColumn(row, null, false);
+                            DataColumn? c = PreviousColumn(row, null, false);
                             if (c != null)
                             {
                                 MoveTo(parent, c, _doc.IsTextOnly(c));
@@ -851,8 +849,8 @@ namespace System.Xml
             AssertValid();
             if (_node != null)
             {
-                DataRow curRow = null;
-                XmlNode parent = null;
+                DataRow? curRow = null;
+                XmlNode? parent = null;
                 if (_column != null)
                 {
                     curRow = Row;
@@ -869,7 +867,7 @@ namespace System.Xml
                 //first check with the columns in the row
                 if (curRow != null)
                 {
-                    DataColumn c = NextColumn(curRow, null, false);
+                    DataColumn? c = NextColumn(curRow, null, false);
                     while (c != null)
                     {
                         if (IsValidChild(_node, c))
@@ -881,7 +879,7 @@ namespace System.Xml
                     }
                 }
                 //didn't find a valid column or maybe already Foliated, go through its children nodes
-                XmlNode n = _doc.SafeFirstChild(parent);
+                XmlNode? n = _doc.SafeFirstChild(parent);
                 while (n != null)
                 {
                     if (IsValidChild(parent, n))
@@ -913,8 +911,8 @@ namespace System.Xml
                 if (!IsFoliated(_node))
                 {
                     // find virtual column elements first
-                    DataRow curRow = Row;
-                    DataColumn c = NextColumn(curRow, null, false);
+                    DataRow curRow = Row!;
+                    DataColumn? c = NextColumn(curRow, null, false);
                     while (c != null)
                     {
                         if (IsValidChild(_node, c))
@@ -923,7 +921,7 @@ namespace System.Xml
                     }
                 }
                 // look for anything
-                XmlNode n = _doc.SafeFirstChild(_node);
+                XmlNode? n = _doc.SafeFirstChild(_node);
                 while (n != null)
                 {
                     if (IsValidChild(_node, n))
@@ -954,8 +952,8 @@ namespace System.Xml
             if (!IsFoliated(_node))
             {
                 // find virtual column elements first
-                DataRow curRow = Row;
-                DataColumn c = NextColumn(curRow, null, false);
+                DataRow curRow = Row!;
+                DataColumn? c = NextColumn(curRow, null, false);
                 while (c != null)
                 {
                     if (IsValidChild(_node, c))
@@ -967,7 +965,7 @@ namespace System.Xml
                 }
             }
             // look for anything
-            XmlNode n = _doc.SafeFirstChild(_node);
+            XmlNode? n = _doc.SafeFirstChild(_node);
             while (n != null)
             {
                 if (IsValidChild(_node, n))
@@ -988,7 +986,7 @@ namespace System.Xml
             AssertValid();
             if (NodeType == XPathNodeType.Namespace)
             {
-                MoveTo(_parentOfNS);
+                MoveTo(_parentOfNS!);
                 return true;
             }
             if (_node != null)
@@ -1005,7 +1003,7 @@ namespace System.Xml
                 }
                 else
                 {
-                    XmlNode n = null;
+                    XmlNode? n = null;
                     if (_node.NodeType == XmlNodeType.Attribute)
                         n = ((XmlAttribute)_node).OwnerElement;
                     else
@@ -1020,7 +1018,7 @@ namespace System.Xml
             return false;
         }
 
-        private XmlNode GetParent(XmlNode node)
+        private XmlNode? GetParent(XmlNode node)
         {
             XPathNodeType xnt = ConvertNodeType(node);
             if (xnt == XPathNodeType.Namespace)
@@ -1036,7 +1034,7 @@ namespace System.Xml
         internal void MoveToRoot()
         {
             XmlNode node = _node;
-            XmlNode parent = _node;
+            XmlNode? parent = _node;
             while (parent != null)
             {
                 node = parent;
@@ -1066,8 +1064,8 @@ namespace System.Xml
 
         private XmlNodeOrder CompareNamespacePosition(XPathNodePointer other)
         {
-            XPathNodePointer xp1 = Clone((DataDocumentXPathNavigator)(_owner.Target));
-            XPathNodePointer xp2 = other.Clone((DataDocumentXPathNavigator)(other._owner.Target));
+            XPathNodePointer xp1 = Clone((DataDocumentXPathNavigator)(_owner.Target!));
+            XPathNodePointer xp2 = other.Clone((DataDocumentXPathNavigator)(other._owner.Target!));
             while (xp1.MoveToNextNamespace(XPathNamespaceScope.All))
             {
                 if (xp1.IsSamePosition(xp2))
@@ -1080,7 +1078,7 @@ namespace System.Xml
         {
             depth = 0;
             XmlNode curNode = node;
-            XmlNode parent = ((curNode.NodeType == XmlNodeType.Attribute) ? (((XmlAttribute)curNode).OwnerElement) : (curNode.ParentNode));
+            XmlNode? parent = ((curNode.NodeType == XmlNodeType.Attribute) ? (((XmlAttribute)curNode).OwnerElement) : (curNode.ParentNode));
             for (; parent != null; depth++)
             {
                 curNode = parent;
@@ -1096,7 +1094,7 @@ namespace System.Xml
 
             if (IsSamePosition(other))
                 return XmlNodeOrder.Same;
-            XmlNode curNode1 = null, curNode2 = null;
+            XmlNode? curNode1 = null, curNode2 = null;
 
             //deal with namespace node first
             if (NodeType == XPathNodeType.Namespace && other.NodeType == XPathNodeType.Namespace)
@@ -1204,9 +1202,9 @@ namespace System.Xml
                     return XmlNodeOrder.Before;
             }
 
-            XmlNode parent1 = GetParent(curNode1);
-            XmlNode parent2 = GetParent(curNode2);
-            XmlNode nextNode = null;
+            XmlNode? parent1 = GetParent(curNode1!);
+            XmlNode? parent2 = GetParent(curNode2!);
+            XmlNode? nextNode = null;
             while (parent1 != null && parent2 != null)
             {
                 if (parent1 == parent2)
@@ -1230,7 +1228,7 @@ namespace System.Xml
             return XmlNodeOrder.Unknown;
         }
 
-        internal XmlNode Node
+        internal XmlNode? Node
         {
             get
             {
@@ -1240,7 +1238,7 @@ namespace System.Xml
                 if (_node == null)
                     return null;
 
-                XmlBoundElement rowElem = GetRowElement();
+                XmlBoundElement? rowElem = GetRowElement();
                 if (rowElem != null)
                 {
                     bool wasFoliationEnabled = _doc.IsFoliationEnabled;
@@ -1260,7 +1258,7 @@ namespace System.Xml
             return nodeToCheck == _node;
         }
 
-        bool IXmlDataVirtualNode.IsOnColumn(DataColumn col)
+        bool IXmlDataVirtualNode.IsOnColumn(DataColumn? col)
         {
             RealFoliate();
             return col == _column;
@@ -1287,7 +1285,7 @@ namespace System.Xml
 
             Debug.Assert(_column != null);
 
-            XmlNode n = null;
+            XmlNode? n = null;
 
             if (_doc.IsTextOnly(_column))
                 n = _node.FirstChild;
@@ -1295,7 +1293,7 @@ namespace System.Xml
             {
                 if (_column.ColumnMapping == MappingType.Attribute)
                 {
-                    n = _node.Attributes.GetNamedItem(_column.EncodedColumnName, _column.Namespace);
+                    n = _node.Attributes!.GetNamedItem(_column.EncodedColumnName, _column.Namespace);
                 }
                 else
                 {
@@ -1324,11 +1322,11 @@ namespace System.Xml
 
         //The function only helps to find out if there is a namespace declaration of given name is defined on the given node
         //It will not check the ancestor of the given node.
-        private string GetNamespace(XmlBoundElement be, string name)
+        private string? GetNamespace(XmlBoundElement be, string name)
         {
             if (be == null)
                 return null;
-            XmlAttribute attr = null;
+            XmlAttribute? attr = null;
             if (be.IsFoliated)
             {
                 attr = be.GetAttributeNode(name, StrReservedXmlns);
@@ -1339,11 +1337,11 @@ namespace System.Xml
             }
             else
             { //defoliated so that we need to search through its column
-                DataRow curRow = be.Row;
+                DataRow? curRow = be.Row;
                 if (curRow == null)
                     return null;
                 //going through its attribute columns
-                DataColumn curCol = PreviousColumn(curRow, null, true);
+                DataColumn? curCol = PreviousColumn(curRow, null, true);
                 while (curCol != null)
                 {
                     if (curCol.Namespace == StrReservedXmlns)
@@ -1367,9 +1365,9 @@ namespace System.Xml
             if (name != null && name.Length == 0)
                 name = "xmlns";
             RealFoliate();
-            XmlNode node = _node;
+            XmlNode? node = _node;
             XmlNodeType nt = node.NodeType;
-            string retVal = null;
+            string? retVal = null;
             while (node != null)
             {
                 //first identify an element node in the ancestor + itself
@@ -1384,7 +1382,7 @@ namespace System.Xml
                 if (node != null)
                 {
                     //must be element node
-                    retVal = GetNamespace((XmlBoundElement)node, name);
+                    retVal = GetNamespace((XmlBoundElement)node, name!);
                     if (retVal != null)
                         return retVal;
                     //didn't find it, try the next parentnode
@@ -1407,9 +1405,9 @@ namespace System.Xml
             if (attrName != null && attrName.Length == 0)
                 attrName = "xmlns";
             RealFoliate();
-            XmlNode node = _node;
-            XmlAttribute attr = null;
-            XmlBoundElement be = null;
+            XmlNode? node = _node;
+            XmlAttribute? attr = null;
+            XmlBoundElement? be = null;
             while (node != null)
             {
                 //check current element node
@@ -1427,11 +1425,11 @@ namespace System.Xml
                     }
                     else
                     {//defoliated so that we need to search through its column
-                        DataRow curRow = be.Row;
+                        DataRow? curRow = be.Row;
                         if (curRow == null)
                             return false;
                         //going through its attribute columns
-                        DataColumn curCol = PreviousColumn(curRow, null, true);
+                        DataColumn? curCol = PreviousColumn(curRow, null, true);
                         while (curCol != null)
                         {
                             if (curCol.Namespace == StrReservedXmlns && curCol.ColumnName == name)
@@ -1456,14 +1454,14 @@ namespace System.Xml
 
         //the function will find the next namespace node on the given bound element starting with the given column or attribute
         // whether to use column or attribute depends on if the bound element is foliated or not.
-        private bool MoveToNextNamespace(XmlBoundElement be, DataColumn col, XmlAttribute curAttr)
+        private bool MoveToNextNamespace(XmlBoundElement? be, DataColumn? col, XmlAttribute? curAttr)
         {
             if (be != null)
             {
                 if (be.IsFoliated)
                 {
                     XmlAttributeCollection attrs = be.Attributes;
-                    XmlAttribute attr = null;
+                    XmlAttribute? attr = null;
                     bool bFound = false;
                     if (curAttr == null)
                         bFound = true; //the first namespace will be the one
@@ -1488,11 +1486,11 @@ namespace System.Xml
                 }
                 else
                 {//defoliated so that we need to search through its column
-                    DataRow curRow = be.Row;
+                    DataRow? curRow = be.Row;
                     if (curRow == null)
                         return false;
                     //going through its attribute columns
-                    DataColumn curCol = PreviousColumn(curRow, col, true);
+                    DataColumn? curCol = PreviousColumn(curRow, col, true);
                     while (curCol != null)
                     {
                         if (curCol.Namespace == StrReservedXmlns && !DuplicateNS(be, curCol.ColumnName))
@@ -1515,8 +1513,8 @@ namespace System.Xml
             //only need to check with _node, even if _column is not null and its mapping type is element, it can't have attributes
             if (_parentOfNS == null)
                 return false;
-            XmlNode node = _node;
-            XmlBoundElement be = null;
+            XmlNode? node = _node;
+            XmlBoundElement? be = null;
             while (node != null)
             {
                 be = node as XmlBoundElement;
@@ -1547,8 +1545,8 @@ namespace System.Xml
         {
             if (_parentOfNS == null || endElem == null)
                 return false;
-            XmlBoundElement be = _parentOfNS;
-            XmlNode node = null;
+            XmlBoundElement? be = _parentOfNS;
+            XmlNode? node = null;
             while (be != null && be != endElem)
             {
                 if (GetNamespace(be, lname) != null)
@@ -1568,18 +1566,18 @@ namespace System.Xml
         {
             RealFoliate();
             Debug.Assert(_parentOfNS != null);
-            XmlNode node = _node;
+            XmlNode? node = _node;
             //first check within the same boundelement
             if (_column != null)
             {
                 Debug.Assert(_column.Namespace == StrReservedXmlns);
                 if (namespaceScope == XPathNamespaceScope.Local && _parentOfNS != _node) //already outside scope
                     return false;
-                XmlBoundElement be = _node as XmlBoundElement;
+                XmlBoundElement? be = _node as XmlBoundElement;
                 Debug.Assert(be != null);
-                DataRow curRow = be.Row;
+                DataRow? curRow = be.Row;
                 Debug.Assert(curRow != null);
-                DataColumn curCol = PreviousColumn(curRow, _column, true);
+                DataColumn? curCol = PreviousColumn(curRow, _column, true);
                 while (curCol != null)
                 {
                     if (curCol.Namespace == StrReservedXmlns)
@@ -1621,7 +1619,7 @@ namespace System.Xml
             while (node != null)
             {
                 //try the namespace attributes from the same element
-                XmlBoundElement be = node as XmlBoundElement;
+                XmlBoundElement? be = node as XmlBoundElement;
                 if (MoveToNextNamespace(be, null, null))
                     return true;
                 //no more namespace attribute under the same element
@@ -1648,10 +1646,10 @@ namespace System.Xml
             if (_column != null)
             {
                 // We must be on a de-foliated region
-                XmlBoundElement rowElem = _node as XmlBoundElement;
+                XmlBoundElement? rowElem = _node as XmlBoundElement;
                 Debug.Assert(rowElem != null);
 
-                DataRow row = rowElem.Row;
+                DataRow? row = rowElem.Row;
                 Debug.Assert(row != null);
 
                 // We cannot be on a column for which the value is DBNull
index 77ed74c..ae57ad3 100644 (file)
@@ -1,9 +1,6 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-// TODO: Enable after System.Private.Xml is annotated
-#nullable disable
-
 using System.Data;
 using System.Diagnostics;
 using System.Threading;
@@ -24,7 +21,7 @@ namespace System.Xml
 
     internal sealed class XmlBoundElement : XmlElement
     {
-        private DataRow _row;
+        private DataRow? _row;
         private ElementState _state;
 
         internal XmlBoundElement(string prefix, string localName, string namespaceURI, XmlDocument doc) : base(prefix, localName, namespaceURI, doc)
@@ -43,7 +40,7 @@ namespace System.Xml
 
         public override bool HasAttributes => Attributes.Count > 0;
 
-        public override XmlNode FirstChild
+        public override XmlNode? FirstChild
         {
             get
             {
@@ -52,9 +49,9 @@ namespace System.Xml
             }
         }
 
-        internal XmlNode SafeFirstChild => base.FirstChild;
+        internal XmlNode? SafeFirstChild => base.FirstChild;
 
-        public override XmlNode LastChild
+        public override XmlNode? LastChild
         {
             get
             {
@@ -63,14 +60,14 @@ namespace System.Xml
             }
         }
 
-        public override XmlNode PreviousSibling
+        public override XmlNode? PreviousSibling
         {
             get
             {
-                XmlNode prev = base.PreviousSibling;
+                XmlNode? prev = base.PreviousSibling;
                 if (prev == null)
                 {
-                    XmlBoundElement parent = ParentNode as XmlBoundElement;
+                    XmlBoundElement? parent = ParentNode as XmlBoundElement;
                     if (parent != null)
                     {
                         parent.AutoFoliate();
@@ -81,16 +78,16 @@ namespace System.Xml
             }
         }
 
-        internal XmlNode SafePreviousSibling => base.PreviousSibling;
+        internal XmlNode? SafePreviousSibling => base.PreviousSibling;
 
-        public override XmlNode NextSibling
+        public override XmlNode? NextSibling
         {
             get
             {
-                XmlNode next = base.NextSibling;
+                XmlNode? next = base.NextSibling;
                 if (next == null)
                 {
-                    XmlBoundElement parent = ParentNode as XmlBoundElement;
+                    XmlBoundElement? parent = ParentNode as XmlBoundElement;
                     if (parent != null)
                     {
                         parent.AutoFoliate();
@@ -101,7 +98,7 @@ namespace System.Xml
             }
         }
 
-        internal XmlNode SafeNextSibling => base.NextSibling;
+        internal XmlNode? SafeNextSibling => base.NextSibling;
 
         public override bool HasChildNodes
         {
@@ -112,13 +109,13 @@ namespace System.Xml
             }
         }
 
-        public override XmlNode InsertBefore(XmlNode newChild, XmlNode refChild)
+        public override XmlNode? InsertBefore(XmlNode newChild, XmlNode? refChild)
         {
             AutoFoliate();
             return base.InsertBefore(newChild, refChild);
         }
 
-        public override XmlNode InsertAfter(XmlNode newChild, XmlNode refChild)
+        public override XmlNode? InsertAfter(XmlNode newChild, XmlNode? refChild)
         {
             AutoFoliate();
             return base.InsertAfter(newChild, refChild);
@@ -130,7 +127,7 @@ namespace System.Xml
             return base.ReplaceChild(newChild, oldChild);
         }
 
-        public override XmlNode AppendChild(XmlNode newChild)
+        public override XmlNode? AppendChild(XmlNode newChild)
         {
             AutoFoliate();
             return base.AppendChild(newChild);
@@ -138,8 +135,8 @@ namespace System.Xml
 
         internal void RemoveAllChildren()
         {
-            XmlNode child = FirstChild;
-            XmlNode sibling = null;
+            XmlNode? child = FirstChild;
+            XmlNode? sibling = null;
 
             while (child != null)
             {
@@ -173,7 +170,7 @@ namespace System.Xml
             }
         }
 
-        internal DataRow Row
+        internal DataRow? Row
         {
             get { return _row; }
             set { _row = value; }
index 7c8ce6d..09d6ee7 100644 (file)
@@ -1,14 +1,12 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-// TODO: Enable after System.Private.Xml is annotated
-#nullable disable
-
 using System.Collections;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
 using System.IO;
 using System.Xml.XPath;
 
@@ -62,14 +60,14 @@ namespace System.Xml
                     ArrayList al = new ArrayList();
                     foreach (DictionaryEntry entry in _pointers)
                     {
-                        IXmlDataVirtualNode temp = (IXmlDataVirtualNode)(entry.Value);
+                        IXmlDataVirtualNode? temp = (IXmlDataVirtualNode?)(entry.Value);
                         Debug.Assert(temp != null);
                         if (!temp.IsInUse())
                             al.Add(temp);
                     }
                     for (int i = 0; i < al.Count; i++)
                     {
-                        _pointers.Remove(al[i]);
+                        _pointers.Remove(al[i]!);
                     }
                     _countAddPointer = 0;
                 }
@@ -81,7 +79,7 @@ namespace System.Xml
         internal void AssertPointerPresent(IXmlDataVirtualNode pointer)
         {
 #if DEBUG
-            object val = _pointers[pointer];
+            object? val = _pointers[pointer];
             if (val != (object)pointer)
                 Debug.Fail("Pointer not present");
 #endif
@@ -95,6 +93,7 @@ namespace System.Xml
         //  - ds.ReadXmlData();             // ds is now filled, however doc has no content (since there were no listeners for the new created DataRow's)
         // We can set-up listeners and track each change in schema, but it is more perf-friendly to do it laizily, all at once, when the first DataRow is created
         // (we rely on the fact that DataRowCreated is a DataSet wide event, rather than a DataTable event)
+        [MemberNotNull(nameof(_dataSet))]
         private void AttachDataSet(DataSet ds)
         {
             // You should not have already an associated dataset
@@ -110,12 +109,12 @@ namespace System.Xml
 
         // after loading, all detached DataRows are synchronized with the xml tree and inserted to their tables
         // or after setting the innerxml, synchronize the rows and if created new and detached, will be inserted.
-        internal void SyncRows(DataRow parentRow, XmlNode node, bool fAddRowsToTable)
+        internal void SyncRows(DataRow? parentRow, XmlNode node, bool fAddRowsToTable)
         {
-            XmlBoundElement be = node as XmlBoundElement;
+            XmlBoundElement? be = node as XmlBoundElement;
             if (be != null)
             {
-                DataRow r = be.Row;
+                DataRow? r = be.Row;
                 if (r != null && be.ElementState == ElementState.Defoliated)
                     return; //no need of syncRow
 
@@ -137,7 +136,7 @@ namespace System.Xml
             }
 
             // Attach all rows from children nodes
-            for (XmlNode child = node.FirstChild; child != null; child = child.NextSibling)
+            for (XmlNode? child = node.FirstChild; child != null; child = child.NextSibling)
                 SyncRows(parentRow, child, fAddRowsToTable);
         }
 
@@ -145,14 +144,14 @@ namespace System.Xml
         // Synchronize the rows and if created new and detached, will be inserted.
         internal void SyncTree(XmlNode node)
         {
-            XmlBoundElement be = null;
+            XmlBoundElement? be = null;
             _mapper.GetRegion(node, out be);
-            DataRow parentRow = null;
+            DataRow? parentRow = null;
             bool fAddRowsToTable = IsConnected(node);
 
             if (be != null)
             {
-                DataRow r = be.Row;
+                DataRow? r = be.Row;
                 if (r != null && be.ElementState == ElementState.Defoliated)
                     return; //no need of syncRow
 
@@ -177,7 +176,7 @@ namespace System.Xml
             }
 
             // Attach all rows from children nodes
-            for (XmlNode child = node.FirstChild; child != null; child = child.NextSibling)
+            for (XmlNode? child = node.FirstChild; child != null; child = child.NextSibling)
                 SyncRows(parentRow, child, fAddRowsToTable);
         }
 
@@ -328,7 +327,7 @@ namespace System.Xml
         /// Creates an element with the specified Prefix, LocalName, and
         /// NamespaceURI.
         /// </summary>
-        public override XmlElement CreateElement(string prefix, string localName, string namespaceURI)
+        public override XmlElement CreateElement(string? prefix, string localName, string? namespaceURI)
         {
             // There are three states for the document:
             //  - special listeners ON, no permananent listeners: this is when the data doc was created w/o any dataset, and the 1st time a new row/element
@@ -370,7 +369,7 @@ namespace System.Xml
                     if (col.ColumnMapping != MappingType.Hidden)
                         SetRowValueToNull(row, col);
                 }
-                XmlBoundElement be = row.Element;
+                XmlBoundElement? be = row.Element;
                 Debug.Assert(be != null);
                 be.Prefix = prefix;
                 return be;
@@ -422,12 +421,12 @@ namespace System.Xml
                 // drop all attributes
                 rowElem.RemoveAllAttributes();
 
-                XmlNode node = rowElem.FirstChild;
+                XmlNode? node = rowElem.FirstChild;
                 while (node != null)
                 {
-                    XmlNode next = node.NextSibling;
+                    XmlNode? next = node.NextSibling;
 
-                    XmlBoundElement be = node as XmlBoundElement;
+                    XmlBoundElement? be = node as XmlBoundElement;
                     if (be != null && be.Row != null)
                         break;
 
@@ -455,7 +454,7 @@ namespace System.Xml
 
         private XmlElement EnsureDocumentElement()
         {
-            XmlElement docelem = DocumentElement;
+            XmlElement? docelem = DocumentElement;
             if (docelem == null)
             {
                 string docElemName = XmlConvert.EncodeLocalName(DataSet.DataSetName);
@@ -472,11 +471,11 @@ namespace System.Xml
         }
         private XmlElement EnsureNonRowDocumentElement()
         {
-            XmlElement docElem = DocumentElement;
+            XmlElement? docElem = DocumentElement;
             if (docElem == null)
                 return EnsureDocumentElement();
 
-            DataRow rowDocElem = GetRowFromElement(docElem);
+            DataRow? rowDocElem = GetRowFromElement(docElem);
             if (rowDocElem == null)
                 return docElem;
 
@@ -490,7 +489,7 @@ namespace System.Xml
             Debug.Assert(GetRowFromElement(DocumentElement) != null);
 
             // Remove the DocumentElement and create a new one
-            XmlElement oldDocElem = DocumentElement;
+            XmlElement oldDocElem = DocumentElement!;
             RemoveChild(oldDocElem);
             XmlElement docElem = EnsureDocumentElement();
             docElem.AppendChild(oldDocElem);
@@ -544,7 +543,7 @@ namespace System.Xml
             {
                 foreach (DataRow r in row.GetChildRows(dr))
                 {
-                    XmlElement childElem = r.Element;
+                    XmlElement? childElem = r.Element;
                     // childElem can be null when we create XML from DataSet (XmlDataDocument( DataSet ) is called) and we insert rowElem of the parentRow before
                     // we insert the rowElem of children rows.
                     if (childElem != null)
@@ -554,7 +553,7 @@ namespace System.Xml
 #endif
                         if (childElem.ParentNode != rowElement)
                         {
-                            childElem.ParentNode.RemoveChild(childElem);
+                            childElem.ParentNode!.RemoveChild(childElem);
                             rowElement.AppendChild(childElem);
                         }
 #if DEBUG
@@ -578,7 +577,7 @@ namespace System.Xml
             {
                 if (newState == ElementState.StrongFoliation && node.Row == null)
                 {
-                    XmlBoundElement rowElem;
+                    XmlBoundElement? rowElem;
                     ElementState rowElemState = ElementState.None;
                     if (_mapper.GetRegion(node, out rowElem))
                     {
@@ -659,7 +658,7 @@ namespace System.Xml
 
                 try
                 {
-                    XmlNode priorNode = null;
+                    XmlNode? priorNode = null;
                     DataRow row = node.Row;
 
                     // create new attrs & elements for row
@@ -681,7 +680,7 @@ namespace System.Xml
                                 }
                                 else
                                 {
-                                    XmlNode newNode = null;
+                                    XmlNode? newNode = null;
                                     if (col.ColumnMapping == MappingType.Element)
                                     {
                                         newNode = new XmlBoundElement(string.Empty, col.EncodedColumnName, col.Namespace, this);
@@ -737,10 +736,10 @@ namespace System.Xml
         }
 
         //Determine best radical insert position for inserting column elements
-        private XmlNode GetColumnInsertAfterLocation(DataRow row, DataColumn col, XmlBoundElement rowElement)
+        private XmlNode? GetColumnInsertAfterLocation(DataRow row, DataColumn col, XmlBoundElement rowElement)
         {
-            XmlNode prev = null;
-            XmlNode node = null;
+            XmlNode? prev = null;
+            XmlNode? node = null;
 
             // text only columns appear first
             if (IsTextOnly(col))
@@ -758,13 +757,13 @@ namespace System.Xml
                 // insert location must be before any non-element nodes
                 if (node.NodeType != XmlNodeType.Element)
                     break;
-                XmlElement e = node as XmlElement;
+                XmlElement? e = node as XmlElement;
 
                 // insert location must be before any non-mapped elements or separate regions
                 if (_mapper.GetRowFromElement(e) != null)
                     break;
 
-                object schema = _mapper.GetColumnSchemaForNode(rowElement, node);
+                object? schema = _mapper.GetColumnSchemaForNode(rowElement, node);
                 if (schema == null || !(schema is DataColumn))
                     break;
 
@@ -789,15 +788,15 @@ namespace System.Xml
             return list;
         }
 
-        private DataRow GetNestedParent(DataRow row)
+        private DataRow? GetNestedParent(DataRow row)
         {
-            DataRelation relation = GetNestedParentRelation(row);
+            DataRelation? relation = GetNestedParentRelation(row);
             if (relation != null)
                 return row.GetParentRow(relation);
             return null;
         }
 
-        private static DataRelation GetNestedParentRelation(DataRow row)
+        private static DataRelation? GetNestedParentRelation(DataRow row)
         {
             DataRelation[] relations = row.Table.NestedParentRelations;
             if (relations.Length == 0)
@@ -805,7 +804,7 @@ namespace System.Xml
             return relations[0];
         }
 
-        private DataColumn GetTextOnlyColumn(DataRow row)
+        private DataColumn? GetTextOnlyColumn(DataRow row)
         {
 #if DEBUG
             {
@@ -833,12 +832,12 @@ namespace System.Xml
         /// <summary>
         /// Retrieves the DataRow associated with the specified XmlElement.
         /// </summary>
-        public DataRow GetRowFromElement(XmlElement e)
+        public DataRow? GetRowFromElement(XmlElement? e)
         {
             return _mapper.GetRowFromElement(e);
         }
 
-        private XmlNode GetRowInsertBeforeLocation(DataRow row, XmlElement rowElement, XmlNode parentElement)
+        private XmlNode? GetRowInsertBeforeLocation(DataRow row, XmlElement rowElement, XmlNode parentElement)
         {
             DataRow refRow = row;
             int i = 0;
@@ -851,7 +850,7 @@ namespace System.Xml
                     break;
             pos = i;
 
-            DataRow parentRow = GetNestedParent(row);
+            DataRow? parentRow = GetNestedParent(row);
             for (i = pos + 1; i < row.Table.Rows.Count; i++)
             {
                 refRow = row.Table.Rows[i];
@@ -871,7 +870,7 @@ namespace System.Xml
         /// </summary>
         public XmlElement GetElementFromRow(DataRow r)
         {
-            XmlBoundElement be = r.Element;
+            XmlBoundElement? be = r.Element;
             Debug.Assert(be != null);
             return be;
         }
@@ -884,7 +883,7 @@ namespace System.Xml
                 {
                     if (_pointers.Count > 0)
                     {
-                        object pointer = null;
+                        object? pointer = null;
                         foreach (DictionaryEntry entry in _pointers)
                         {
                             pointer = entry.Value;
@@ -997,7 +996,7 @@ namespace System.Xml
                         dp.MoveToOwnerElement();
                         if (dp.MoveToAttribute(i))
                         {
-                            newNode.Attributes.Append((XmlAttribute)CloneTreeInternal(dp));
+                            newNode.Attributes!.Append((XmlAttribute)CloneTreeInternal(dp));
                         }
                     }
 
@@ -1053,12 +1052,12 @@ namespace System.Xml
                 //for the nodes without value and have no children
                 XmlNodeType.DocumentFragment => CreateDocumentFragment(),
                 XmlNodeType.DocumentType => CreateDocumentType(dp.Name, dp.PublicId, dp.SystemId, dp.InternalSubset),
-                XmlNodeType.XmlDeclaration => CreateXmlDeclaration(dp.Version, dp.Encoding, dp.Standalone),
+                XmlNodeType.XmlDeclaration => CreateXmlDeclaration(dp.Version!, dp.Encoding, dp.Standalone),
 
                 //for the nodes with value but no children
                 XmlNodeType.Text => CreateTextNode(dp.Value),
                 XmlNodeType.CDATA => CreateCDataSection(dp.Value),
-                XmlNodeType.ProcessingInstruction => CreateProcessingInstruction(dp.Name, dp.Value),
+                XmlNodeType.ProcessingInstruction => CreateProcessingInstruction(dp.Name, dp.Value!),
                 XmlNodeType.Comment => CreateComment(dp.Value),
                 XmlNodeType.Whitespace => CreateWhitespace(dp.Value),
                 XmlNodeType.SignificantWhitespace => CreateSignificantWhitespace(dp.Value),
@@ -1259,18 +1258,18 @@ namespace System.Xml
         }
 
         // load all data from tree structre into datarows
-        private void LoadRows(XmlBoundElement rowElem, XmlNode node)
+        private void LoadRows(XmlBoundElement? rowElem, XmlNode node)
         {
             Debug.Assert(node != null);
 
-            XmlBoundElement be = node as XmlBoundElement;
+            XmlBoundElement? be = node as XmlBoundElement;
             if (be != null)
             {
-                DataTable dt = _mapper.SearchMatchingTableSchema(rowElem, be);
+                DataTable? dt = _mapper.SearchMatchingTableSchema(rowElem, be);
 
                 if (dt != null)
                 {
-                    DataRow r = GetRowFromElement(be);
+                    DataRow? r = GetRowFromElement(be);
                     Debug.Assert(r == null);
                     // If the rowElement was just created and has an un-initialized
                     if (be.ElementState == ElementState.None)
@@ -1284,7 +1283,7 @@ namespace System.Xml
                 }
             }
             // recurse down for children
-            for (XmlNode child = node.FirstChild; child != null; child = child.NextSibling)
+            for (XmlNode? child = node.FirstChild; child != null; child = child.NextSibling)
                 LoadRows(rowElem, child);
         }
 
@@ -1302,7 +1301,7 @@ namespace System.Xml
             OnNewRow(row);
         }
 
-        internal void OnClearCalled(object oDataSet, DataTable table)
+        internal void OnClearCalled(object oDataSet, DataTable? table)
         {
             throw new NotSupportedException(SR.DataDom_NotSupport_Clear);
         }
@@ -1361,10 +1360,10 @@ namespace System.Xml
                 ForceFoliation(rowElement, AutoFoliationState);
 
             Debug.Assert(rowElement != null);
-            DataRow rowDocElem = GetRowFromElement(DocumentElement);
+            DataRow? rowDocElem = GetRowFromElement(DocumentElement);
             if (rowDocElem != null)
             {
-                DataRow parentRow = GetNestedParent(row);
+                DataRow? parentRow = GetNestedParent(row);
                 if (parentRow == null)
                     DemoteDocumentElement();
             }
@@ -1402,7 +1401,7 @@ namespace System.Xml
                         {
                             if (_pointers.Count > 0)
                             {
-                                object pointer = null;
+                                object? pointer = null;
                                 foreach (DictionaryEntry entry in _pointers)
                                 {
                                     pointer = entry.Value;
@@ -1426,7 +1425,7 @@ namespace System.Xml
                 {
                     value = string.Empty;
                     //make sure that rowElement has Attribute xsi:nil and its value is true
-                    XmlAttribute attr = rowElement.GetAttributeNode(XSI_NIL);
+                    XmlAttribute? attr = rowElement.GetAttributeNode(XSI_NIL);
                     if (attr == null)
                     {
                         attr = CreateAttribute(XSI, Keywords.XSI_NIL, Keywords.XSINS);
@@ -1440,7 +1439,7 @@ namespace System.Xml
                 else
                 {
                     //make sure that if rowElement has Attribute xsi:nil, its value is false
-                    XmlAttribute attr = rowElement.GetAttributeNode(XSI_NIL);
+                    XmlAttribute? attr = rowElement.GetAttributeNode(XSI_NIL);
                     if (attr != null)
                         attr.Value = Keywords.FALSE;
                 }
@@ -1460,7 +1459,7 @@ namespace System.Xml
                     {
                         if (Convert.IsDBNull(value))
                         {
-                            attr.OwnerElement.Attributes.Remove(attr);
+                            attr.OwnerElement!.Attributes.Remove(attr);
                         }
                         else
                         {
@@ -1484,12 +1483,12 @@ namespace System.Xml
                 bool fMore = iter.Next();
                 while (fMore)
                 {
-                    if (iter.CurrentNode.NodeType == XmlNodeType.Element)
+                    if (iter.CurrentNode!.NodeType == XmlNodeType.Element)
                     {
                         XmlElement e = (XmlElement)iter.CurrentNode;
                         Debug.Assert(e != null);
                         //we should skip the subregion
-                        XmlBoundElement be = e as XmlBoundElement;
+                        XmlBoundElement? be = e as XmlBoundElement;
                         if (be != null && be.Row != null)
                         {
                             fMore = iter.NextRight(); //skip over the sub-region
@@ -1502,7 +1501,7 @@ namespace System.Xml
                             {
                                 PromoteNonValueChildren(e);
                                 fMore = iter.NextRight();
-                                e.ParentNode.RemoveChild(e);
+                                e.ParentNode!.RemoveChild(e);
                                 // keep looking for more matching elements
                                 continue;
                             }
@@ -1510,7 +1509,7 @@ namespace System.Xml
                             {
                                 ReplaceInitialChildText(e, col.ConvertObjectToXml(value));
                                 //make sure that if the Element has Attribute xsi:nil, its value is false
-                                XmlAttribute attr = e.GetAttributeNode(XSI_NIL);
+                                XmlAttribute? attr = e.GetAttributeNode(XSI_NIL);
                                 if (attr != null)
                                     attr.Value = Keywords.FALSE;
                                 // no need to look any further.
@@ -1527,7 +1526,7 @@ namespace System.Xml
                     XmlElement newElem = new XmlBoundElement(string.Empty, col.EncodedColumnName, col.Namespace, this);
                     newElem.AppendChild(CreateTextNode(col.ConvertObjectToXml(value)));
 
-                    XmlNode elemBefore = GetColumnInsertAfterLocation(row, col, rowElement);
+                    XmlNode? elemBefore = GetColumnInsertAfterLocation(row, col, rowElement);
                     if (elemBefore != null)
                     {
                         rowElement.InsertAfter(newElem, elemBefore);
@@ -1544,7 +1543,7 @@ namespace System.Xml
             }
         lblDoNestedRelationSync:
             // Change the XML to conform to the (potentially) change in parent nested relation
-            DataRelation relation = GetNestedParentRelation(row);
+            DataRelation? relation = GetNestedParentRelation(row);
             if (relation != null)
             {
                 Debug.Assert(relation.ChildTable == row.Table);
@@ -1569,16 +1568,16 @@ namespace System.Xml
             try
             {
                 DataRow row = args.Row;
-                DataColumn col = args.Column;
+                DataColumn? col = args.Column;
 
                 if (row.RowState == DataRowState.Detached)
                 {
-                    XmlBoundElement be = row.Element;
+                    XmlBoundElement? be = row.Element;
                     Debug.Assert(be != null);
                     if (be.IsFoliated)
                     {
                         // Need to sync changes from ROM to DOM
-                        OnColumnValueChanged(row, col, be);
+                        OnColumnValueChanged(row, col!, be);
                     }
                 }
             }
@@ -1598,7 +1597,7 @@ namespace System.Xml
             // but there will be multiple ROM events
             if (_columnChangeList.Count > 0)
             {
-                if (((DataColumn)(_columnChangeList[0])).Table == row.Table)
+                if (((DataColumn)(_columnChangeList[0]!)).Table == row.Table)
                 {
                     foreach (DataColumn c in _columnChangeList)
                         OnColumnValueChanged(row, c, rowElement);
@@ -1627,7 +1626,7 @@ namespace System.Xml
                 DemoteDocumentElement();
 
             PromoteInnerRegions(rowElement);
-            rowElement.ParentNode.RemoveChild(rowElement);
+            rowElement.ParentNode!.RemoveChild(rowElement);
         }
 
         private void OnDeletingRow(DataRow row, XmlBoundElement rowElement)
@@ -1663,7 +1662,7 @@ namespace System.Xml
                     {
                         foreach (DictionaryEntry entry in _pointers)
                         {
-                            object pointer = entry.Value;
+                            object? pointer = entry.Value;
                             Debug.Assert(pointer != null);
                             ((IXmlDataVirtualNode)pointer).OnFoliated(node);
                         }
@@ -1679,7 +1678,7 @@ namespace System.Xml
             // You should never get here in regular cases
         }
 
-        private DataColumn FindAssociatedParentColumn(DataRelation relation, DataColumn childCol)
+        private DataColumn? FindAssociatedParentColumn(DataRelation relation, DataColumn childCol)
         {
             DataColumn[] columns = relation.ChildKey.ColumnsReference;
             for (int i = 0; i < columns.Length; i++)
@@ -1691,7 +1690,7 @@ namespace System.Xml
         }
 
         // Change the childElement position in the tree to conform to the parent nested relationship in ROM
-        private void OnNestedParentChange(DataRow child, XmlBoundElement childElement, DataColumn childCol)
+        private void OnNestedParentChange(DataRow child, XmlBoundElement childElement, DataColumn? childCol)
         {
             Debug.Assert(child.Element == childElement && childElement.Row == child);
             // This function is (and s/b) called as a result of ROM changes, therefore XML changes done here should not be sync-ed to ROM
@@ -1700,12 +1699,12 @@ namespace System.Xml
             // In order to check that this move does not change the connected/disconnected state of the node
             bool fChildElementConnected = IsConnected(childElement);
 #endif
-            DataRow parentRowInTree;
+            DataRow? parentRowInTree;
             if (childElement == DocumentElement || childElement.ParentNode == null)
                 parentRowInTree = null;
             else
                 parentRowInTree = GetRowFromElement((XmlElement)childElement.ParentNode);
-            DataRow parentRowInRelation = GetNestedParent(child);
+            DataRow? parentRowInRelation = GetNestedParent(child!);
 
             if (parentRowInTree != parentRowInRelation)
             {
@@ -1717,17 +1716,17 @@ namespace System.Xml
                 else
                 {
                     // no parent? Maybe the parentRow is during changing or childCol is the ID is set to null ( detached from the parent row ).
-                    DataRelation relation = GetNestedParentRelation(child);
+                    DataRelation? relation = GetNestedParentRelation(child!);
                     if (childCol == null || relation == null || Convert.IsDBNull(child[childCol]))
                     {
                         EnsureNonRowDocumentElement().AppendChild(childElement);
                     }
                     else
                     {
-                        DataColumn colInParent = FindAssociatedParentColumn(relation, childCol);
+                        DataColumn? colInParent = FindAssociatedParentColumn(relation, childCol);
                         Debug.Assert(colInParent != null);
-                        object comparedValue = colInParent.ConvertValue(child[childCol]);
-                        if (parentRowInTree._tempRecord != -1 && colInParent.CompareValueTo(parentRowInTree._tempRecord, comparedValue) != 0)
+                        object? comparedValue = colInParent.ConvertValue(child[childCol]);
+                        if (parentRowInTree!._tempRecord != -1 && colInParent.CompareValueTo(parentRowInTree._tempRecord, comparedValue) != 0)
                         {
                             EnsureNonRowDocumentElement().AppendChild(childElement);
                         }
@@ -1761,7 +1760,7 @@ namespace System.Xml
             try
             {
                 // okay to allow text node value changes when bound.
-                XmlBoundElement rowElement = null;
+                XmlBoundElement? rowElement = null;
 
                 Debug.Assert(DataSet.EnforceConstraints == false);
 
@@ -1808,9 +1807,9 @@ namespace System.Xml
             try
             {
                 // Handle both new node inserted and 2nd part of a move operation.
-                XmlNode node = args.Node;
-                XmlNode oldParent = args.OldParent;
-                XmlNode newParent = args.NewParent;
+                XmlNode node = args.Node!;
+                XmlNode? oldParent = args.OldParent;
+                XmlNode? newParent = args.NewParent;
 
                 // The code bellow assumes a move operation is fired by DOM in 2 steps: a Remvoe followed by an Insert - this is the 2nd part, the Insert.
                 Debug.Assert(oldParent == null);
@@ -1862,8 +1861,8 @@ namespace System.Xml
 
             try
             {
-                XmlNode node = args.Node;
-                XmlNode oldParent = args.OldParent;
+                XmlNode node = args.Node!;
+                XmlNode? oldParent = args.OldParent;
                 Debug.Assert(args.NewParent == null);
 
                 if (IsConnected(oldParent))
@@ -1895,22 +1894,22 @@ namespace System.Xml
         }
 
         // Node was removed from connected tree to disconnected tree
-        private void OnNodeRemovedFromTree(XmlNode node, XmlNode oldParent)
+        private void OnNodeRemovedFromTree(XmlNode node, XmlNode? oldParent)
         {
-            XmlBoundElement oldRowElem;
+            XmlBoundElement? oldRowElem;
 
             // Synchronize values from old region
             if (_mapper.GetRegion(oldParent, out oldRowElem))
                 SynchronizeRowFromRowElement(oldRowElem);
 
             // Disconnect all regions, starting w/ node (if it is a row-elem)
-            XmlBoundElement rowElem = node as XmlBoundElement;
+            XmlBoundElement? rowElem = node as XmlBoundElement;
             if (rowElem != null && rowElem.Row != null)
                 EnsureDisconnectedDataRow(rowElem);
             TreeIterator iter = new TreeIterator(node);
             for (bool fMore = iter.NextRowElement(); fMore; fMore = iter.NextRowElement())
             {
-                rowElem = (XmlBoundElement)(iter.CurrentNode);
+                rowElem = (XmlBoundElement)(iter.CurrentNode!);
                 EnsureDisconnectedDataRow(rowElem);
             }
 
@@ -1918,22 +1917,22 @@ namespace System.Xml
             AssertNonLiveRows(node);
         }
         // Node was removed from the disconnected tree to disconnected tree
-        private void OnNodeRemovedFromFragment(XmlNode node, XmlNode oldParent)
+        private void OnNodeRemovedFromFragment(XmlNode node, XmlNode? oldParent)
         {
-            XmlBoundElement oldRowElem;
+            XmlBoundElement? oldRowElem;
 
             if (_mapper.GetRegion(oldParent, out oldRowElem))
             {
                 // Sync the old region if it is not deleted
-                DataRow row = oldRowElem.Row;
+                DataRow row = oldRowElem.Row!;
                 // Since the old region was disconnected, then the row can be only Deleted or Detached
                 Debug.Assert(!IsRowLive(row));
-                if (oldRowElem.Row.RowState == DataRowState.Detached)
+                if (oldRowElem.Row!.RowState == DataRowState.Detached)
                     SynchronizeRowFromRowElement(oldRowElem);
             }
 
             // Need to set nested for the sub-regions (if node is a row-elem, we need to set it just for itself)
-            XmlBoundElement be = node as XmlBoundElement;
+            XmlBoundElement? be = node as XmlBoundElement;
             if (be != null && be.Row != null)
             {
                 Debug.Assert(!IsRowLive(be.Row));
@@ -1945,7 +1944,7 @@ namespace System.Xml
                 TreeIterator iter = new TreeIterator(node);
                 for (bool fMore = iter.NextRowElement(); fMore; fMore = iter.NextRightRowElement())
                 {
-                    XmlBoundElement rowElemChild = (XmlBoundElement)(iter.CurrentNode);
+                    XmlBoundElement rowElemChild = (XmlBoundElement)(iter.CurrentNode!);
                     SetNestedParentRegion(rowElemChild, null);
                 }
             }
@@ -1967,7 +1966,7 @@ namespace System.Xml
             try
             {
                 DataRow row = args.Row;
-                XmlBoundElement rowElement = row.Element;
+                XmlBoundElement? rowElement = row.Element;
                 // We should have an associated row-elem created when the DataRow was created (or at the load time)
                 Debug.Assert(rowElement != null);
 
@@ -1990,7 +1989,7 @@ namespace System.Xml
                                 break;
 
                             case DataRowState.Added:
-                                rowElement.ParentNode.RemoveChild(rowElement);
+                                rowElement.ParentNode!.RemoveChild(rowElement);
                                 break;
 
                             case DataRowState.Modified:
@@ -2130,12 +2129,12 @@ namespace System.Xml
             }
         }
 
-        private void OnDataSetPropertyChanging(object oDataSet, PropertyChangedEventArgs args)
+        private void OnDataSetPropertyChanging(object? oDataSet, PropertyChangedEventArgs args)
         {
             if (args.PropertyName == "DataSetName")
                 throw new InvalidOperationException(SR.DataDom_DataSetNameChange);
         }
-        private void OnColumnPropertyChanging(object oColumn, PropertyChangedEventArgs args)
+        private void OnColumnPropertyChanging(object? oColumn, PropertyChangedEventArgs args)
         {
             if (args.PropertyName == "ColumnName")
                 throw new InvalidOperationException(SR.DataDom_ColumnNameChange);
@@ -2144,7 +2143,7 @@ namespace System.Xml
             if (args.PropertyName == "ColumnMapping")
                 throw new InvalidOperationException(SR.DataDom_ColumnMappingChange);
         }
-        private void OnTablePropertyChanging(object oTable, PropertyChangedEventArgs args)
+        private void OnTablePropertyChanging(object? oTable, PropertyChangedEventArgs args)
         {
             if (args.PropertyName == "TableName")
                 throw new InvalidOperationException(SR.DataDom_TableNameChange);
@@ -2180,7 +2179,7 @@ namespace System.Xml
                 throw new InvalidOperationException(SR.DataDom_DataSetNestedRelationsChange);
 
             // If Add and Remove, we should already been throwing if .Nested == false
-            Debug.Assert(!(args.Action == CollectionChangeAction.Add || args.Action == CollectionChangeAction.Remove) || rel.Nested == false);
+            Debug.Assert(!(args.Action == CollectionChangeAction.Add || args.Action == CollectionChangeAction.Remove) || rel!.Nested == false);
             if (args.Action == CollectionChangeAction.Refresh)
             {
                 foreach (DataRelation relTemp in (DataRelationCollection)oRelationsCollection)
@@ -2193,7 +2192,7 @@ namespace System.Xml
             }
         }
 
-        private void OnRelationPropertyChanging(object oRelationsCollection, PropertyChangedEventArgs args)
+        private void OnRelationPropertyChanging(object? oRelationsCollection, PropertyChangedEventArgs args)
         {
             if (args.PropertyName == "Nested")
                 throw new InvalidOperationException(SR.DataDom_DataSetNestedRelationsChange);
@@ -2201,7 +2200,7 @@ namespace System.Xml
 
         private void OnUndeleteRow(DataRow row, XmlElement rowElement)
         {
-            XmlNode refRow;
+            XmlNode? refRow;
             XmlElement parent;
 
             // make certain we weren't place somewhere else.
@@ -2209,7 +2208,7 @@ namespace System.Xml
                 rowElement.ParentNode.RemoveChild(rowElement);
 
             // Find the parent of RowNode to be inserted
-            DataRow parentRowInRelation = GetNestedParent(row);
+            DataRow? parentRowInRelation = GetNestedParent(row);
             if (parentRowInRelation == null)
             {
                 parent = EnsureNonRowDocumentElement();
@@ -2252,7 +2251,7 @@ namespace System.Xml
             Debug.Assert(parent != DocumentElement);                  // We cannot promote children of the DocumentElement
 
             XmlNode prevSibling = parent;
-            XmlBoundElement parentRegionRowElem;
+            XmlBoundElement? parentRegionRowElem;
             _mapper.GetRegion(parent.ParentNode, out parentRegionRowElem);
 
             TreeIterator iter = new TreeIterator(parent);
@@ -2271,9 +2270,9 @@ namespace System.Xml
         {
             Debug.Assert(parent != null);
             XmlNode prevSibling = parent;
-            XmlNode child = parent.FirstChild;
+            XmlNode? child = parent.FirstChild;
             bool bTextLikeNode = true;
-            XmlNode nextSibling = null;
+            XmlNode? nextSibling = null;
             while (child != null)
             {
                 nextSibling = child.NextSibling;
@@ -2288,19 +2287,19 @@ namespace System.Xml
             }
         }
 
-        private void RemoveInitialTextNodes(XmlNode node)
+        private void RemoveInitialTextNodes(XmlNode? node)
         {
             while (node != null && IsTextLikeNode(node))
             {
-                XmlNode sibling = node.NextSibling;
-                node.ParentNode.RemoveChild(node);
+                XmlNode? sibling = node.NextSibling;
+                node.ParentNode!.RemoveChild(node);
                 node = sibling;
             }
         }
 
         private void ReplaceInitialChildText(XmlNode parent, string value)
         {
-            XmlNode n = parent.FirstChild;
+            XmlNode? n = parent.FirstChild;
 
             // don't consider whitespace when replacing initial text
             while (n != null && n.NodeType == XmlNodeType.Whitespace)
@@ -2312,7 +2311,7 @@ namespace System.Xml
                     n.Value = value;
                 else
                     n = parent.InsertBefore(CreateTextNode(value), n);
-                RemoveInitialTextNodes(n.NextSibling);
+                RemoveInitialTextNodes(n!.NextSibling);
             }
             else
             {
@@ -2320,9 +2319,9 @@ namespace System.Xml
             }
         }
 
-        internal XmlNode SafeFirstChild(XmlNode n)
+        internal XmlNode? SafeFirstChild(XmlNode n)
         {
-            XmlBoundElement be = n as XmlBoundElement;
+            XmlBoundElement? be = n as XmlBoundElement;
             if (be != null)
                 return be.SafeFirstChild;
             else
@@ -2330,9 +2329,9 @@ namespace System.Xml
                 return n.FirstChild;
         }
 
-        internal XmlNode SafeNextSibling(XmlNode n)
+        internal XmlNode? SafeNextSibling(XmlNode n)
         {
-            XmlBoundElement be = n as XmlBoundElement;
+            XmlBoundElement? be = n as XmlBoundElement;
             if (be != null)
                 return be.SafeNextSibling;
             else
@@ -2340,9 +2339,9 @@ namespace System.Xml
                 return n.NextSibling;
         }
 
-        internal XmlNode SafePreviousSibling(XmlNode n)
+        internal XmlNode? SafePreviousSibling(XmlNode n)
         {
-            XmlBoundElement be = n as XmlBoundElement;
+            XmlBoundElement? be = n as XmlBoundElement;
             if (be != null)
                 return be.SafePreviousSibling;
             else
@@ -2363,7 +2362,7 @@ namespace System.Xml
         internal static void SetRowValueFromXmlText(DataRow row, DataColumn col, string xmlText)
         {
             Debug.Assert(xmlText != null);
-            Debug.Assert(row.Table.DataSet.EnforceConstraints == false);
+            Debug.Assert(row.Table.DataSet!.EnforceConstraints == false);
             object oVal;
             try
             {
@@ -2388,9 +2387,9 @@ namespace System.Xml
         }
         // Sync row fields w/ values from rowElem region.
         // If rowElemList is != null, all subregions of rowElem are appended to it.
-        private void SynchronizeRowFromRowElement(XmlBoundElement rowElement, ArrayList rowElemList)
+        private void SynchronizeRowFromRowElement(XmlBoundElement rowElement, ArrayList? rowElemList)
         {
-            DataRow row = rowElement.Row;
+            DataRow? row = rowElement.Row;
             Debug.Assert(row != null);
 
             // No synchronization needed for deleted rows
@@ -2425,7 +2424,7 @@ namespace System.Xml
             }
 #endif
         }
-        private void SynchronizeRowFromRowElementEx(XmlBoundElement rowElement, ArrayList rowElemList)
+        private void SynchronizeRowFromRowElementEx(XmlBoundElement rowElement, ArrayList? rowElemList)
         {
             Debug.Assert(rowElement != null);
             Debug.Assert(rowElement.Row != null);
@@ -2440,7 +2439,7 @@ namespace System.Xml
             RegionIterator iter = new RegionIterator(rowElement);
             bool fMore;
             // If present, fill up the TextOnly column
-            DataColumn column = GetTextOnlyColumn(row);
+            DataColumn? column = GetTextOnlyColumn(row);
             if (column != null)
             {
                 foundColumns[column] = column;
@@ -2457,14 +2456,14 @@ namespace System.Xml
             // Fill up the columns mapped to an element
             while (fMore)
             {
-                XmlElement e = iter.CurrentNode as XmlElement;
+                XmlElement? e = iter.CurrentNode as XmlElement;
                 if (e == null)
                 {
                     fMore = iter.Next();
                     continue;
                 }
 
-                XmlBoundElement be = e as XmlBoundElement;
+                XmlBoundElement? be = e as XmlBoundElement;
                 if (be != null && be.Row != null)
                 {
                     if (rowElemList != null)
@@ -2474,7 +2473,7 @@ namespace System.Xml
                     continue;
                 }
 
-                DataColumn c = _mapper.GetColumnSchemaForNode(rowElement, e);
+                DataColumn? c = _mapper.GetColumnSchemaForNode(rowElement, e);
                 if (c != null)
                 {
                     Debug.Assert(c.Table == row.Table);
@@ -2499,7 +2498,7 @@ namespace System.Xml
             //
             foreach (XmlAttribute attr in rowElement.Attributes)
             {
-                DataColumn c = _mapper.GetColumnSchemaForNode(rowElement, attr);
+                DataColumn? c = _mapper.GetColumnSchemaForNode(rowElement, attr);
 
                 if (c != null)
                 {
@@ -2553,8 +2552,21 @@ namespace System.Xml
 
         internal XmlDataDocument(XmlImplementation imp) : base(imp)
         {
-        }
-
+            // This constructor is used by XmlDataImplementation.CreateDocument(), which
+            // exposes it as XmlDocument. The methods using these fields are never called.
+            _dataSet = null!;
+            _pointers = null!;
+            _columnChangeList = null!;
+            _mapper = null!;
+            _foliationLock = null!;
+            _attrXml = null!;
+        }
+
+        [MemberNotNull(nameof(_pointers))]
+        [MemberNotNull(nameof(_columnChangeList))]
+        [MemberNotNull(nameof(_mapper))]
+        [MemberNotNull(nameof(_foliationLock))]
+        [MemberNotNull(nameof(_attrXml))]
         private void Init()
         {
             _pointers = new Hashtable();
@@ -2574,6 +2586,12 @@ namespace System.Xml
             _ignoreXmlEvents = false;
         }
 
+        [MemberNotNull(nameof(_pointers))]
+        [MemberNotNull(nameof(_columnChangeList))]
+        [MemberNotNull(nameof(_mapper))]
+        [MemberNotNull(nameof(_foliationLock))]
+        [MemberNotNull(nameof(_attrXml))]
+        [MemberNotNull(nameof(_dataSet))]
         private void Init(DataSet ds)
         {
             if (ds == null)
@@ -2586,7 +2604,7 @@ namespace System.Xml
             Bind(true);
         }
 
-        private bool IsConnected(XmlNode node)
+        private bool IsConnected(XmlNode? node)
         {
             while (true)
             {
@@ -2595,7 +2613,7 @@ namespace System.Xml
                 if (node == this)
                     return true;
 
-                XmlAttribute attr = node as XmlAttribute;
+                XmlAttribute? attr = node as XmlAttribute;
                 if (attr != null)
                     node = attr.OwnerElement;
                 else
@@ -2606,9 +2624,9 @@ namespace System.Xml
         {
             return (row.RowState & (DataRowState.Added | DataRowState.Unchanged | DataRowState.Modified)) != 0;
         }
-        private static void SetNestedParentRow(DataRow childRow, DataRow parentRow)
+        private static void SetNestedParentRow(DataRow childRow, DataRow? parentRow)
         {
-            DataRelation rel = GetNestedParentRelation(childRow);
+            DataRelation? rel = GetNestedParentRelation(childRow);
             //we should not set this row's parentRow if the table doesn't match.
             if (rel != null)
             {
@@ -2622,7 +2640,7 @@ namespace System.Xml
         // A node (node) was inserted into the main tree (connected) from oldParent==null state
         private void OnNodeInsertedInTree(XmlNode node)
         {
-            XmlBoundElement be;
+            XmlBoundElement? be;
             ArrayList rowElemList = new ArrayList();
             if (_mapper.GetRegion(node, out be))
             {
@@ -2647,7 +2665,7 @@ namespace System.Xml
             while (rowElemList.Count > 0)
             {
                 Debug.Assert(rowElemList[0] != null && rowElemList[0] is XmlBoundElement);
-                XmlBoundElement subRowElem = (XmlBoundElement)(rowElemList[0]);
+                XmlBoundElement? subRowElem = (XmlBoundElement?)(rowElemList[0]);
                 rowElemList.RemoveAt(0);
                 // Expect rowElem to have a DataTable schema, since it is a sub-region
                 Debug.Assert(subRowElem != null);
@@ -2660,12 +2678,12 @@ namespace System.Xml
         // "node" was inserting into a disconnected tree from oldParent==null state
         private void OnNodeInsertedInFragment(XmlNode node)
         {
-            XmlBoundElement be;
+            XmlBoundElement? be;
             if (_mapper.GetRegion(node, out be))
             {
                 if (be == node)
                 {
-                    Debug.Assert(!IsRowLive(be.Row));
+                    Debug.Assert(!IsRowLive(be.Row!));
                     SetNestedParentRegion(be);
                 }
                 else
@@ -2676,7 +2694,7 @@ namespace System.Xml
                     while (rowElemList.Count > 0)
                     {
                         Debug.Assert(rowElemList[0] != null && rowElemList[0] is XmlBoundElement);
-                        XmlBoundElement subRowElem = (XmlBoundElement)(rowElemList[0]);
+                        XmlBoundElement subRowElem = (XmlBoundElement)(rowElemList[0]!);
                         rowElemList.RemoveAt(0);
                         SetNestedParentRegion(subRowElem, be);
                     }
@@ -2708,7 +2726,7 @@ namespace System.Xml
 #if DEBUG
                     try
                     {
-                        Debug.Assert(row.Table.DataSet.EnforceConstraints == false);
+                        Debug.Assert(row.Table.DataSet!.EnforceConstraints == false);
 #endif
                         row.Table.Rows.Add(row);
                         SetNestedParentRegion(rowElem);
@@ -2732,7 +2750,7 @@ namespace System.Xml
 #if DEBUG
                     try
                     {
-                        Debug.Assert(row.Table.DataSet.EnforceConstraints == false);
+                        Debug.Assert(row.Table.DataSet!.EnforceConstraints == false);
 #endif
                         // Change the row status to be alive (unchanged)
                         row.RejectChanges();
@@ -2770,7 +2788,7 @@ namespace System.Xml
 #if DEBUG
                     try
                     {
-                        Debug.Assert(row.Table.DataSet.EnforceConstraints == false);
+                        Debug.Assert(row.Table.DataSet!.EnforceConstraints == false);
 #endif
                         SetNestedParentRegion(rowElem);
 #if DEBUG
@@ -2812,7 +2830,7 @@ namespace System.Xml
         private void OnNonRowElementInsertedInTree(XmlNode node, XmlBoundElement rowElement, ArrayList rowElemList)
         {
             // non-row-elem is beeing inserted
-            DataRow row = rowElement.Row;
+            DataRow? row = rowElement.Row;
             // Region should already have an associated data row (otherwise how was the original row-elem inserted ?)
             Debug.Assert(row != null);
             SynchronizeRowFromRowElement(rowElement);
@@ -2828,7 +2846,7 @@ namespace System.Xml
         private void OnNonRowElementInsertedInFragment(XmlNode node, XmlBoundElement rowElement, ArrayList rowElemList)
         {
             // non-row-elem is beeing inserted
-            DataRow row = rowElement.Row;
+            DataRow? row = rowElement.Row;
             // Region should already have an associated data row (otherwise how was the original row-elem inserted ?)
             Debug.Assert(row != null);
             // Since oldParent == null, the only 2 row states should have been Detached or Deleted
@@ -2843,21 +2861,21 @@ namespace System.Xml
         {
             Debug.Assert(childRowElem.Row != null);
 
-            XmlBoundElement parentRowElem;
+            XmlBoundElement? parentRowElem;
             _mapper.GetRegion(childRowElem.ParentNode, out parentRowElem);
             SetNestedParentRegion(childRowElem, parentRowElem);
         }
-        private void SetNestedParentRegion(XmlBoundElement childRowElem, XmlBoundElement parentRowElem)
+        private void SetNestedParentRegion(XmlBoundElement childRowElem, XmlBoundElement? parentRowElem)
         {
-            DataRow childRow = childRowElem.Row;
+            DataRow childRow = childRowElem.Row!;
             if (parentRowElem == null)
             {
                 SetNestedParentRow(childRow, null);
                 return;
             }
 
+            Debug.Assert(parentRowElem.Row != null);
             DataRow parentRow = parentRowElem.Row;
-            Debug.Assert(parentRow != null);
             // We should set it only if there is a nested relationship between this child and parent regions
             DataRelation[] relations = childRow.Table.NestedParentRelations;
             if (relations.Length != 0 && relations[0].ParentTable == parentRow.Table) // just backward compatable
@@ -2908,25 +2926,25 @@ namespace System.Xml
         }
         */
 
-        protected override XPathNavigator CreateNavigator(XmlNode node)
+        protected override XPathNavigator? CreateNavigator(XmlNode node)
         {
             Debug.Assert(node.OwnerDocument == this || node == this);
             if (XPathNodePointer.s_xmlNodeType_To_XpathNodeType_Map[(int)(node.NodeType)] == -1)
                 return null;
             if (IsTextNode(node.NodeType))
             {
-                XmlNode parent = node.ParentNode;
+                XmlNode? parent = node.ParentNode;
                 if (parent != null && parent.NodeType == XmlNodeType.Attribute)
                     return null;
                 else
                 {
 #if DEBUG
                     //if current node is a text node, its parent node has to be foliated
-                    XmlBoundElement be = node.ParentNode as XmlBoundElement;
+                    XmlBoundElement? be = node.ParentNode as XmlBoundElement;
                     if (be != null)
                         Debug.Assert(be.IsFoliated);
 #endif
-                    XmlNode prevSib = node.PreviousSibling;
+                    XmlNode? prevSib = node.PreviousSibling;
                     while (prevSib != null && IsTextNode(prevSib.NodeType))
                     {
                         node = prevSib;
@@ -2944,14 +2962,14 @@ namespace System.Xml
             IsFoliationEnabled = false;
             try
             {
-                XmlBoundElement rowElement = node as XmlBoundElement;
+                XmlBoundElement? rowElement = node as XmlBoundElement;
                 if (rowElement != null && rowElement.Row != null)
                     Debug.Assert(IsRowLive(rowElement.Row));
                 TreeIterator iter = new TreeIterator(node);
                 for (bool fMore = iter.NextRowElement(); fMore; fMore = iter.NextRowElement())
                 {
                     rowElement = iter.CurrentNode as XmlBoundElement;
-                    Debug.Assert(rowElement.Row != null);
+                    Debug.Assert(rowElement!.Row != null);
                     Debug.Assert(IsRowLive(rowElement.Row));
                 }
             }
@@ -2967,14 +2985,14 @@ namespace System.Xml
             IsFoliationEnabled = false;
             try
             {
-                XmlBoundElement rowElement = node as XmlBoundElement;
+                XmlBoundElement? rowElement = node as XmlBoundElement;
                 if (rowElement != null && rowElement.Row != null)
                     Debug.Assert(!IsRowLive(rowElement.Row));
                 TreeIterator iter = new TreeIterator(node);
                 for (bool fMore = iter.NextRowElement(); fMore; fMore = iter.NextRowElement())
                 {
                     rowElement = iter.CurrentNode as XmlBoundElement;
-                    Debug.Assert(rowElement.Row != null);
+                    Debug.Assert(rowElement!.Row != null);
                     Debug.Assert(!IsRowLive(rowElement.Row));
                 }
             }
@@ -2984,7 +3002,7 @@ namespace System.Xml
             }
         }
 
-        public override XmlElement GetElementById(string elemId)
+        public override XmlElement? GetElementById(string elemId)
         {
             throw new NotSupportedException(SR.DataDom_NotSupport_GetElementById);
         }
@@ -3002,9 +3020,9 @@ namespace System.Xml
         //  after adding Namespace support foir datatable, DataSet does not guarantee that infered tabels would be in the same sequence as they rae in XML, because
         //  of Namespace. if a table is in different namespace than its children and DataSet, that table would efinetely be added to DataSet after its children. Its By Design
         // so in order to maintain backward compatability, we reorder the copy of the datatable collection and use it
-        private DataTable[] OrderTables(DataSet ds)
+        private DataTable[] OrderTables(DataSet? ds)
         {
-            DataTable[] retValue = null;
+            DataTable[]? retValue = null;
             if (ds == null || ds.Tables.Count == 0)
             {
                 retValue = Array.Empty<DataTable>();
@@ -3019,7 +3037,7 @@ namespace System.Xml
 
             if (null == retValue)
             {
-                retValue = new DataTable[ds.Tables.Count];
+                retValue = new DataTable[ds!.Tables.Count];
                 List<DataTable> tableList = new List<DataTable>();
                 // first take the root tables that have no parent
                 foreach (DataTable dt in ds.Tables)
index 957c0c6..a161154 100644 (file)
@@ -1,9 +1,6 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
-// TODO: Enable after System.Private.Xml is annotated
-#nullable disable
-
 #pragma warning disable 618 // ignore obsolete warning about XmlDataDocument
 
 namespace System.Xml