这是一些字节顺序标记问题吗

这是一些字节顺序标记问题吗

我的问题似乎来自不同操作系统中文本文件之间的差异。所以我会在这里碰碰运气。

我想尝试从创建的默认项目.NET Core 模板 在 Ubuntu 18.04 上。我已经安装了 SQL Servermssql-server_15.0.4003.23-3_amd64.deb并选择 Express 作为版本。由于 LocalDB 仅适用于 Windows,因此我认为必须修改src/WebUI/appsettings.json以将 LocalDB 连接字符串替换为以下 SQL Server 连接字符串(我不确定它是否正确):

  "ConnectionStrings": {
      "DefaultConnection": "Server=localhost\\SQLEXPRESS,1433;Database=ExampleDb;User Id=sa;Password=password;"
  },

然后我开始src/WebUI/构建它但失败了(见下文)。这些错误似乎与我对连接字符串的更改无关。

关于第一个错误,我src/Domain/Domain.csproj在Emacs中打开,它显示了一些奇怪的东西<U+FEFF>,其中https://en.wikipedia.org/wiki/Byte_order_mark说的是关于字节顺序。我想知道如何使这些文件正常工作?

<U+FEFF>namespace default.Domain.Enums
{
    public enum PriorityLevel
    {
        None,
        Low,
        Medium,
        High
    }
}

谢谢。

$ dotnet build
Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 147.56 ms for /tmp/test/default/src/Domain/Domain.csproj.
  Restore completed in 213.91 ms for /tmp/test/default/src/Application/Application.csproj.
  Restore completed in 57.04 ms for /tmp/test/default/src/Infrastructure/Infrastructure.csproj.
  Restore completed in 9.65 ms for /tmp/test/default/src/WebUI/WebUI.csproj.
Enums/PriorityLevel.cs(1,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Enums/PriorityLevel.cs(1,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Enums/PriorityLevel.cs(1,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Enums/PriorityLevel.cs(1,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Enums/PriorityLevel.cs(2,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Exceptions/AdAccountInvalidException.cs(3,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Exceptions/AdAccountInvalidException.cs(3,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Exceptions/AdAccountInvalidException.cs(3,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Exceptions/AdAccountInvalidException.cs(3,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Exceptions/AdAccountInvalidException.cs(4,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(1,7): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(1,7): error CS1002: ; expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(1,22): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(1,7): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(1,28): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(2,7): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(2,7): error CS1002: ; expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(2,22): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(2,7): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(2,32): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(6,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(6,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(6,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(6,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(7,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/ValueObject.cs(4,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/ValueObject.cs(4,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Common/ValueObject.cs(4,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/ValueObject.cs(4,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/ValueObject.cs(5,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(1,7): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(1,7): error CS1002: ; expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(1,22): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(1,7): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(1,28): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(2,7): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(2,7): error CS1002: ; expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(2,22): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(2,7): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(2,27): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(5,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(5,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(5,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(5,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(6,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(1,7): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(1,7): error CS1002: ; expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(1,22): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(1,7): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(1,28): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(4,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(4,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(4,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(4,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(5,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/AuditableEntity.cs(3,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/AuditableEntity.cs(3,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Common/AuditableEntity.cs(3,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/AuditableEntity.cs(3,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/AuditableEntity.cs(4,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]

Build FAILED.

Enums/PriorityLevel.cs(1,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Enums/PriorityLevel.cs(1,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Enums/PriorityLevel.cs(1,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Enums/PriorityLevel.cs(1,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Enums/PriorityLevel.cs(2,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Exceptions/AdAccountInvalidException.cs(3,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Exceptions/AdAccountInvalidException.cs(3,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Exceptions/AdAccountInvalidException.cs(3,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Exceptions/AdAccountInvalidException.cs(3,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Exceptions/AdAccountInvalidException.cs(4,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(1,7): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(1,7): error CS1002: ; expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(1,22): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(1,7): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(1,28): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(2,7): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(2,7): error CS1002: ; expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(2,22): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(2,7): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(2,32): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(6,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(6,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(6,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(6,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
ValueObjects/AdAccount.cs(7,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/ValueObject.cs(4,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/ValueObject.cs(4,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Common/ValueObject.cs(4,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/ValueObject.cs(4,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/ValueObject.cs(5,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(1,7): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(1,7): error CS1002: ; expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(1,22): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(1,7): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(1,28): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(2,7): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(2,7): error CS1002: ; expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(2,22): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(2,7): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(2,27): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(5,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(5,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(5,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(5,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoItem.cs(6,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(1,7): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(1,7): error CS1002: ; expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(1,22): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(1,7): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(1,28): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(4,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(4,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(4,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(4,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Entities/TodoList.cs(5,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/AuditableEntity.cs(3,11): error CS1001: Identifier expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/AuditableEntity.cs(3,26): error CS0116: A namespace cannot directly contain members such as fields or methods [/tmp/test/default/src/Domain/Domain.csproj]
Common/AuditableEntity.cs(3,11): error CS1514: { expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/AuditableEntity.cs(3,18): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
Common/AuditableEntity.cs(4,1): error CS1022: Type or namespace definition, or end-of-file expected [/tmp/test/default/src/Domain/Domain.csproj]
    0 Warning(s)
    60 Error(s)

答案1

在我看来,字节顺序标记有点混乱,并且在现代计算中通常是不必要的。它们最初的用途是 UTF-16 和 UTF-32,为了在不同体系结构的计算机之间进行互换,需要一种方法来区分小端和大端。如今,UTF-16 和 UTF-32 远不如 UTF-8 受欢迎,而 utf-8 则不然需要物料清单。

关于何时使用 BOM 的标准非常模糊:

参见第 67 页:http://www.unicode.org/versions/Unicode12.0.0/UnicodeStandard-12.0.pdf

以 U+FEFF 字节顺序标记开头的数据流(或文件)可能包含 Unicode 字符。建议发送或接收编码字符的非类型化数据流的应用程序使用此签名。如果使用其他信令方法,则不应使用签名。

作为一般规则,大多数数据流都是类型化的。例如utf-8配置文件是预期的为 utf-8 等,根据定义,它们不是“无类型的”。因此,作为一般规则,除非您另有了解,否则不应包含 BOM。

添加 BOM 比删除 BOM 更有可能导致问题。 UTF-8 被设计为向后兼容 ASCII。这允许您在许多只需要 ASCII 的地方使用 UTF-8。但 BOM 不是 ASCII 的一部分,也不是 ASCII,并且许多/大多数读取文本文件的程序不能很好地处理它。大多数人不知道在文件开头忽略它,或者在连接文件时删除它。需要 BOM 的程序通常可以容忍 BOM 的缺失。

然而,许多文本编辑器会在不告诉您的情况下添加 BOM。可能是您在编辑配置文件时无意中添加的。

还有一个问题直接询问如何删除BOM: 如何从 UTF-8 文件中删除 BOM?

我会删除添加到您的配置文件中的任何 BOM。 MS SQL 不太可能期望它们。

答案2

可能是这样的

iconv -fUTF16 -tUTF8 file

相关内容