diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNodeReader.cs b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNodeReader.cs index bfba0b21748c8..de5ca962b7c11 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNodeReader.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNodeReader.cs @@ -1360,32 +1360,23 @@ public override string GetAttribute(int attributeIndex) // Moves to the attribute with the specified name. public override bool MoveToAttribute(string name) { - if (!IsInReadingStates()) - return false; - _readerNav.ResetMove(ref _curDepth, ref _nodeType); - if (_readerNav.MoveToAttribute(name)) - { //, ref curDepth ) ) { - _curDepth++; - _nodeType = _readerNav.NodeType; - if (_bInReadBinary) - { - FinishReadBinary(); - } - return true; - } - _readerNav.RollBackMove(ref _curDepth); - return false; + return MoveToAttribute(name, string.Empty); } // Moves to the attribute with the specified name and namespace. public override bool MoveToAttribute(string name, string? namespaceURI) { if (!IsInReadingStates()) + { return false; + } + + XmlNodeType savedNodeType = _nodeType; + _readerNav.ResetMove(ref _curDepth, ref _nodeType); - string ns = (namespaceURI == null) ? string.Empty : namespaceURI; + string ns = namespaceURI ?? string.Empty; if (_readerNav.MoveToAttribute(name, ns)) - { //, ref curDepth ) ) { + { _curDepth++; _nodeType = _readerNav.NodeType; if (_bInReadBinary) @@ -1395,6 +1386,7 @@ public override bool MoveToAttribute(string name, string? namespaceURI) return true; } _readerNav.RollBackMove(ref _curDepth); + _nodeType = savedNodeType; return false; } diff --git a/src/libraries/System.Private.Xml/tests/XmlNodeReader/System.Xml.XmlNodeReader.Tests/XmlNodeReaderMoveToAttributeTests.cs b/src/libraries/System.Private.Xml/tests/XmlNodeReader/System.Xml.XmlNodeReader.Tests/XmlNodeReaderMoveToAttributeTests.cs index ea32a771a0a86..eb757c92bc7e0 100644 --- a/src/libraries/System.Private.Xml/tests/XmlNodeReader/System.Xml.XmlNodeReader.Tests/XmlNodeReaderMoveToAttributeTests.cs +++ b/src/libraries/System.Private.Xml/tests/XmlNodeReader/System.Xml.XmlNodeReader.Tests/XmlNodeReaderMoveToAttributeTests.cs @@ -121,5 +121,22 @@ public void NodeReaderMoveToElementWithSimpleXml() nodeReader.ReadContentAsBase64(new byte[33], 10, 10); Assert.True(nodeReader.MoveToElement()); } + + [Fact] + public void XmlNodeReaderMoveToUnexistedAttribute() + { + string xml = ""; + + var doc = new XmlDocument(); + doc.LoadXml(xml); + using (var nodeReader = new XmlNodeReader(doc.DocumentElement.FirstChild)) + { + Assert.True(nodeReader.IsStartElement("child")); + Assert.True(nodeReader.MoveToAttribute("attr1")); + Assert.False(nodeReader.MoveToAttribute("attr2")); + Exception exception = Record.Exception(() => nodeReader.ReadStartElement("child")); + Assert.Null(exception); + } + } } }