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
最满意答案
最好的方法是为每个服务(应用程序)使用单独的容器。 通过这种方式,您可以独立于其他容器升级/修改每个服务。 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 ...
-
docker不是VM,为什么容器需要基本映像OS?[关闭](docker is not VM , why container need base image OS ? [closed])[2022-11-27]
你可能想参考这个博文: 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 Hub的情况下将我的应用程序部署到容器操作系统?(How can I deploy my app to a container OS without using Docker Hub?)[2021-08-18]
如果您不想使用注册表,则应该按照以下步骤导出映像: 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服务。 不过,如果 ...