Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid behavior of MoveToAttribute methods #60556

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -1395,6 +1386,7 @@ public override bool MoveToAttribute(string name, string? namespaceURI)
return true;
}
_readerNav.RollBackMove(ref _curDepth);
_nodeType = savedNodeType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do _curDepth or _bInReadBinary need any adjustments? (I've done quick tests with checking XmlReader.Depth and that looks consistent across readers so possibly the answer is no)

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,22 @@ public void NodeReaderMoveToElementWithSimpleXml()
nodeReader.ReadContentAsBase64(new byte[33], 10, 10);
Assert.True(nodeReader.MoveToElement());
}

[Fact]
public void XmlNodeReaderMoveToUnexistedAttribute()
{
string xml = "<root><child attr1='value1'><other /></child></root>";

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);
}
}
}
}