简体   繁体   English

XML 文档 (1, 1) 中存在错误。 反序列化一个简单的 xml 字符串时

[英]There is an error in XML document (1, 1). When deserializing a simple xml string

I checked this post before but it didn't help me(it doesn't have an accepted answer either).我之前检查过这篇文章,但它对我没有帮助(它也没有被接受的答案)。

This test fails:此测试失败:

using System.IO;
using System.Xml.Serialization;
using Xunit;

namespace ATest
{
    public class XmlSerializerTest
    {
        [Fact]
        public void DeserializeTest()
        {
            string request = Serializer.ByteArrayToString(new RegisterRequest{ ... }
               .ToXml());

            RegisterRequest deserialized = request.DeserializeXmlString<RegisterRequest>();

            Assert.NotNull(deserialized);
        }
    }

    public static class Serializer
    {
        public static T DeserializeXmlString<T>(this string xmlObj)
        {
            T outputObject;
            using (StringReader xmlReader = new StringReader(xmlObj))
            {
                var serializer = new XmlSerializer(typeof(T));
                outputObject = (T)serializer.Deserialize(xmlReader); // throws here
            }
            return outputObject;
        }

        public static byte[] ToXml<T>(this T obj)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.Indent = false;

            using MemoryStream ms = new MemoryStream();
            using XmlWriter xmlWriter = XmlWriter.Create(ms, settings);

            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add(String.Empty, String.Empty);

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(xmlWriter, obj, namespaces);

            return ms.ToArray();
        }

        // And convert that byte[] to string with this:
        public static string ByteArrayToString(byte[] bytes) =>
                Encoding.UTF8.GetString(bytes);
    }

    [XmlRoot(elementName: "RegisterRequest")]
    public class RegisterRequest
    {
        [XmlElement]
        public int WorkerId;

        [XmlElement]
        public string WorkerIP;

        [XmlElement]
        public int WorkerPort;
    }
}

The error I get is:我得到的错误是:

System.InvalidOperationException: 'There is an error in XML document (1, 1).' System.InvalidOperationException: 'XML 文档 (1, 1) 中存在错误。

InnerException XmlException: Data at the root level is invalid. InnerException XmlException: 根级别的数据无效。 Line 1, position 1.第 1 行,位置 1。

Problem here is Encoding.UTF8 is UTF-8 encoding with BOM (byte order mark), which means XmlSerializer emits several bytes at the beginning to indicate said byte order mark.这里的问题是Encoding.UTF8是带有 BOM(字节顺序标记)的 UTF-8 编码,这意味着XmlSerializer在开头发出几个字节来指示所述字节顺序标记。 So result of your ToXml contains those non-character bytes at the beginning, and deserialization fails, because deserialization does not expect them.所以你的ToXml结果在开头包含那些非字符字节,反序列化失败,因为反序列化不期望它们。 To fix, tell serializer to not emit BOM by doing this:要修复,请通过执行以下操作告诉序列化程序不要发出 BOM:

XmlWriterSettings settings = new XmlWriterSettings();
//settings.Encoding = Encoding.UTF8;
settings.Encoding = new UTF8Encoding(false); // false means no BOM here

By the way, text you pasted into the question does NOT contain said invisible characters, so with that specific hardcoded string you pasted the problem is not reproducable.顺便说一下,您粘贴到问题中的文本不包含所说的不可见字符,因此对于您粘贴的特定硬编码字符串,问题是不可重现的。 However, since you included the code which produced problem string - one can reproduce it by manually calling ByteArrayToString(ToXml(..)) .但是,由于您包含了产生问题字符串的代码 - 可以通过手动调用ByteArrayToString(ToXml(..))来重现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM