首页 \ 问答 \ SqlServer将XML转换为varbinary并在.NET中解析它(C#)(SqlServer Converting XML to varbinary and parsing it in .NET (C#))

SqlServer将XML转换为varbinary并在.NET中解析它(C#)(SqlServer Converting XML to varbinary and parsing it in .NET (C#))

请考虑以下代码:

[Test]
    public void StackOverflowQuestionTest()
    {
        const string connectionString = "enter your connection string if you wanna test this code";

        byte[] result = null;
        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (var sqlCommand = new SqlCommand("declare @xml as xml = '<xml/>' SELECT convert(varbinary(max), @xml) as value"))
            //using (var sqlCommand = new SqlCommand("SELECT convert(varbinary(max), N'<xml/>') as value"))
            {
                sqlCommand.Connection = connection;

                using (SqlDataReader reader = sqlCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        result = (byte[])reader["value"];
                    }

                    reader.Close();
                }
            }
        }

        string decodedString = new UnicodeEncoding(false, true).GetString(result);
        var document = XElement.Parse(decodedString);
    }

如果我运行此测试,我会收到一条消息的XmlException:“根级别的数据无效。第1行,位置1”。 事实证明问题是“0xFFFE”前导码,它被认为是无效字符。 请注意,如果我使用注释字符串,一切都很好,这对我来说很奇怪。 看起来SqlServer在UCS-2中存储带有BOM的XML字符串,同时它存储没有它的nvarchar值。 主要问题是:如何将此字节数组解码为不包含此前导码(BOM)的字符串?


Consider the following code:

[Test]
    public void StackOverflowQuestionTest()
    {
        const string connectionString = "enter your connection string if you wanna test this code";

        byte[] result = null;
        using (var connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (var sqlCommand = new SqlCommand("declare @xml as xml = '<xml/>' SELECT convert(varbinary(max), @xml) as value"))
            //using (var sqlCommand = new SqlCommand("SELECT convert(varbinary(max), N'<xml/>') as value"))
            {
                sqlCommand.Connection = connection;

                using (SqlDataReader reader = sqlCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        result = (byte[])reader["value"];
                    }

                    reader.Close();
                }
            }
        }

        string decodedString = new UnicodeEncoding(false, true).GetString(result);
        var document = XElement.Parse(decodedString);
    }

If I run this test I get an XmlException with message : "Data at the root level is invalid. Line 1, position 1." As it turns out the problem is "0xFFFE" preamble which is considered as invalid character. Note that if I use commented string instead, everything works just fine, which is strange as per me. Looks like SqlServer stores XML strings in UCS-2 with a BOM, and at the same time it stores nvarchar values without it. The main question is: how can I decode this byte array to string which will not contain this preamble (BOM)?


原文:https://stackoverflow.com/questions/13014709
更新时间:2022-04-17 11:04

最满意答案

最好的方法是为每个服务(应用程序)使用单独的容器。 通过这种方式,您可以独立于其他容器升级/修改每个服务。 Docker非常适合微服务架构。

使用Docker Compose只需一个命令即可运行所有容器: docker-compose up 。 在Docker Compose中,您可以定义容器之间的依赖关系。 因此,它们将自动连接。

如果您想在单独的Linux主机上运行每个容器,您可以使用Docker Swarm ,以便您不必关心网络。


The best way is to use a separate container for each service (application). In this way you can upgrade/modify each service independently from the other containers. Docker works really nice for the micro service architecture.

Use Docker Compose for running all containers with just one command: docker-compose up. In Docker Compose you can define dependencies among containers. So, they will connect automatically.

If you wanna run every container on a separate Linux host, you can use Docker Swarm in order that you don't have to care about networking.

相关问答

更多
  • 如果你看一下docker / java Dockerfile ,它基于: FROM buildpack-deps:jessie-scm buildpack-deps:jessie-scm 取决于 : FROM buildpack-deps:jessie-curl 和buildpack-deps:jessie-curl 取决于 : FROM debian:jessie 这意味着您的tomcat映像基于操作系统( Debian 8 )。 每个Dockerfile都在其基本映像中定义的层上构建。 If yo ...
  • 你可能想参考这个博文: http : //www.activestate.com/blog/2013/06/solomon-hykes-explains-docker 简单地说,Docker采用LXC(一种内置于Linux内核的轻量级虚拟化技术)进行过程隔离。 LXC和VM之间的基本区别在于LXC只有一个Linux内核实例运行。 基本图像OS用于文件系统,二进制文件等。 You might want to refer to this blog post: http://www.activestate.com ...
  • 这个答案中的所有代码都是未经测试的,因为我目前在没有docker的计算机上。 查看容器是否仍在运行 docker ps 停止错误配置的容器 docker stop my-forum-redis docker stop my-forum-nodebb 删除配置错误的容器及其卷 (它们所基于的泊坞窗图像将被保留。) docker rm --volumes --force stop my-forum-nodebb docker rm --volumes --force my-forum-redis 重新开始 ...
  • 默认情况下,这种方式大部分都是这样。 如果您没有expose任何端口并且不在操作系统中运行网络服务(通常只是运行您的应用程序),则容器中无法访问任何内容。 您可以准确地澄清“可达”的含义。 可以从哪里到达目的? 如果您不公开任何端口,则无法从任何其他主机访问您的容器。 您的容器可能仍然可以从主机上的docker网络中的其他容器“到达”,因此如果您关心的是同一个--icc=false主机中的其他--icc=false 容器 , --icc=false提供--icc=false标志来禁用容器间通信 ,默认情况下 ...
  • 如果您不想使用注册表,则应该按照以下步骤导出映像: docker save > .tar scp .tar 然后在你的服务器上呢, docker load < .tar 这会将您的图像加载到服务器中。 You should follow these steps to export the image if you don't want to use a registry: do ...
  • Docker有一个运行容器的虚拟机 不,容器不是虚拟机(docker包含桌面版本的虚拟机,但这不是容器,有虚拟机运行时,但这不是传统的容器)。 容器是一种将正在运行的应用程序与文件系统,pid和网络等内核命名空间隔离的方法。 它们都运行在同一个内核中。 为Alpine,Ubuntu等提供基本映像,为您提供基本文件系统,库,包管理器,但不包括内核。 在容器中启动的唯一过程就是您的应用程序,当您的应用程序退出时,容器也会退出。 因此你不会看到OS实用程序正在运行。 另请参阅此答案 Docker has a VM ...
  • 没有API我知道更改正在运行的容器上的已装载卷。 您销毁现有的容器( docker stop和docker rm )并使用正确的配置( docker run )创建一个新的容器。 如果您发现自己试图维护单个容器,升级容器内的应用程序或内部数据,那么您尝试重新创建VM而不是隔离进程(这是一种反模式)的可能性很大。 从编辑中,您没有创建/ home / deep-learning文件夹,您创建了/ home文件夹。 您似乎还创建了第二个名为deep的容器,没有任何卷装,并将其放入该容器中。 要使容器具有/ ho ...
  • 最好的方法是为每个服务(应用程序)使用单独的容器。 通过这种方式,您可以独立于其他容器升级/修改每个服务。 Docker非常适合微服务架构。 使用Docker Compose只需一个命令即可运行所有容器: docker-compose up 。 在Docker Compose中,您可以定义容器之间的依赖关系。 因此,它们将自动连接。 如果您想在单独的Linux主机上运行每个容器,您可以使用Docker Swarm ,以便您不必关心网络。 The best way is to use a separate c ...
  • 请记住,在Mac / Windows上使用瘦VM,因为Docker需要Linux内核才能运行。 因此,使用--net=host将无法正常工作,实际上这将使用VM的主机网络。 您应该能够通过访问docker VM的端口4000来验证这一点。 对于您的用例,我建议将其他服务移植到docker,如果这不是运行像dnsmasq这样的本地DNS解析器的选项应该做的。 只需让一些域名解析为您的Mac主机IP。 Keep in mind that on Mac/Windows a thin VM is used as D ...
  • 我们正在运行这样的开发配置,其中ZooKeeper,Mesos Masters和Slaves以及Marathon在CoreOS最新稳定版上运行完全停靠(但在3裸机群集上)。 它有一些已知的缺点,例如当一个奴隶死亡时,重新启动的奴隶无法恢复正在运行的任务AFAIK。 我认为这还取决于你在笔记本电脑上运行的操作系统。 如果它不是Windows,你通常应该没问题。 如果您的系统支持systemd ,那么您可以查看tobilg / coreos-setup以了解我如何通过Docker启动Mesos服务。 不过,如果 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)