-
Notifications
You must be signed in to change notification settings - Fork 6k
Fixed incorrect logic in XmlReader's example (#43799) #44054
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
Conversation
@dotnet-policy-service agree |
Converting to review comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @scale-tone sorry for the delay. The post US Thanksgiving and Christmas holiday season has a lot of people in and out of office.
Do you know if the VB code also suffers from this bug? It would have to be fixed too. If you're not interested in doing that, I'll schedule some time to take a look and modify your PR with the fixes. Cheers!
It does, and that's why this PR already contains a fix for VB part as well :) |
OH GEEZ did I not spot that! HAHA |
I totally scrolled down to it too and my brain just saw it as all the same snippet of code. 🤣😂 |
What do you think about modernizing the C# code for the reader too? The static IEnumerable<XElement> StreamRootChildDoc(StringReader stringReader)
{
using XmlReader reader = XmlReader.Create(stringReader);
reader.MoveToContent();
// Parse the file and display each of the nodes.
while (true)
{
// If the current node is an element and named "Child"
if (reader.NodeType == XmlNodeType.Element && reader.Name == "Child")
{
// Get the current node and advance the reader to the next
if (XNode.ReadFrom(reader) is XElement el)
yield return el;
else
continue;
}
else if (!reader.Read())
break;
}
} Instead of static IEnumerable<XElement> StreamRootChildDoc(StringReader stringReader)
{
using (XmlReader reader = XmlReader.Create(stringReader))
{
reader.MoveToContent();
// Parse the file and display each of the nodes.
while (true)
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
if (reader.Name == "Child") {
XElement? el = XNode.ReadFrom(reader) as XElement;
if (el != null)
yield return el;
// Should only call reader.Read(), if XNode.ReadFrom() was NOT called,
// because XNode.ReadFrom() advances the reader to the next element by itself.
continue;
}
break;
}
if (!reader.Read())
break;
}
}
} |
OH man, that VB code is soooooooooo old too. I'm going to update that. |
OK that trimmed about 10 lines from the C# code and almost 70 lines from the VB code! |
Minus two more lines :) :
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a bunch for the fix! Considering this area is low priority (usually based on page views), I'm unsure if we would have gotten to this. your contribution really matters and forces us to take a look. Thanks again!
My pleasure, and happy holidays! :) |
Proper link: Fixes #43799 |
(#43799 is now 3 weeks old without even being triaged, so fixing it myself)
The code sample for XmlReader is incorrect. It relies on the fact that the input XML has some formatting in it. Without formatting the code returns wrong results.
In more details, the issue happens because
XElement.ReadFrom()
reads the current element and positions the reader at the next element. When the XML has formatting, that next element will be of typeWhitespace
or smth - and everything works. But without formatting that next element will be the nextChild
element. Then thereader.Read()
happens, which moves the reader to yet another next element. Hence the<Child Key=""02"">
tag simply gets skipped.Here is the code to address the issue.
Internal previews