Skip to content

Specify XmlException LineNumber and LinePosition are zero-based #3636

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

Merged
merged 8 commits into from
Dec 11, 2019

Conversation

carlossanlop
Copy link
Contributor

@carlossanlop carlossanlop commented Dec 5, 2019

@carlossanlop carlossanlop added the doc-enhancement Improve the current content label Dec 5, 2019
@carlossanlop carlossanlop requested review from jozkee and a team December 5, 2019 23:44
@carlossanlop carlossanlop self-assigned this Dec 5, 2019
@dotnet-bot dotnet-bot added this to the December 2019 milestone Dec 5, 2019
@carlossanlop carlossanlop requested review from buyaa-n and krwq December 5, 2019 23:46
@krwq
Copy link
Member

krwq commented Dec 5, 2019

@carlossanlop I believe we are starting to count with 1. The only thing to mention is that 0 can be encountered when user chooses to not use line info (which is the default). Specific line of code which is mentioned above ThrowWithoutLineInfo does not contain any line number information but since the values backed by int the 0 is probably arbitrary.

Easiest way to check:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Text;

public class Program
{
  public static void Main()
  {
    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("<foo>\r\n<ba</foo>")))
    {
      XDocument xdoc = XDocument.Load(ms, LoadOptions.SetLineInfo);
    }
  }
}

and observe exception:

System.Xml.XmlException: The '<' character, hexadecimal value 0x3C, cannot be included in a name. Line 2, position 4.

@carlossanlop
Copy link
Contributor Author

@krwq thank you for the clarification. I'll adjust the descriptions.

@carlossanlop
Copy link
Contributor Author

carlossanlop commented Dec 6, 2019

Here's an example that throws on line 1:

using System.IO;
using System.Text;
using System.Xml.Linq;

namespace ConsoleApp
{
    public class Program
    {
        public static void Main()
        {
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("<<foo>\r\nbar</foo>")))
            {
                XDocument xdoc = XDocument.Load(ms, LoadOptions.SetLineInfo);
            }
        }
    }
}

Exception message:
System.Xml.XmlException: 'Name cannot begin with the '<' character, hexadecimal value 0x3C. Line 1, position 2.'

@carlossanlop
Copy link
Contributor Author

@mairaw I'm experimenting with adding an example in the Remarks section (without the Remarks title). I recall a few examples where we've done this, and Ron approved a couple of them. What do you think?

@mairaw mairaw removed this from the December 2019 milestone Dec 6, 2019
@mairaw mairaw removed the doc-enhancement Improve the current content label Dec 6, 2019
@mairaw
Copy link
Contributor

mairaw commented Dec 6, 2019

It should be fine. I left a couple of comments for you to consider and apply to the other API as well.

Co-Authored-By: Maira Wenzel <mairaw@microsoft.com>
@carlossanlop
Copy link
Contributor Author

@mairaw
Copy link
Contributor

mairaw commented Dec 6, 2019

I'll make some suggestions. Since what you're showing is anti-pattern, we probably should have this in the remarks instead and just show the little snippet that throws the exception. Makes sense?

Copy link
Contributor

@mairaw mairaw left a comment

Choose a reason for hiding this comment

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

Left some suggestions for you to consider.

@carlossanlop carlossanlop requested a review from mairaw December 7, 2019 01:01
Copy link
Contributor

@mairaw mairaw left a comment

Choose a reason for hiding this comment

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

I still don't think you need the remarks but I'll let you decide that. Left some comments in case you decide to keep them.

Co-Authored-By: Maira Wenzel <mairaw@microsoft.com>
@carlossanlop carlossanlop merged commit 11edd9b into dotnet:master Dec 11, 2019
@carlossanlop carlossanlop deleted the XmlException branch December 11, 2019 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is this 0-based or 1-based?
5 participants