c# - .net XmlSerialize 抛出 "WriteStartDocument cannot be called on writers created with ConformanceLevel.Fragment"

标签 c# .net xml serialization xmlserializer

我正在尝试序列化一个类,将一个 XML 文件作为多个片段写入,即将类的每个对象作为一个单独的片段写入,没有 XML header /根。下面是一个示例代码:

[Serializable]
public class Test
{
    public int X { get; set; }
    public String Y { get; set; }
    public String[] Z { get; set; }

    public Test()
    {
    }

    public Test(int x, String y, String[] z)
    {
        X = x;
        Y = y;
        Z = z;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Test t1 = new Test(1, "t1", new[] { "a", "b" });
        Test t2 = new Test(2, "t2", new[] { "c", "d", "e" });

        XmlSerializer serializer = new XmlSerializer(typeof(Test));
        //using (StreamWriter writer = new StreamWriter(@"f:\test\test.xml"))
        {
            XmlWriter xmlWriter = XmlWriter.Create(@"f:\test\test.xml",
                                                   new XmlWriterSettings()
                                                       {ConformanceLevel = ConformanceLevel.Fragment,
                                                        OmitXmlDeclaration = true,
                                                        Indent = true});
            serializer.Serialize(xmlWriter, t1);
            serializer.Serialize(xmlWriter, t2);
            xmlWriter.Close();
        }
    }
}

在第一次调用序列化时,出现异常:

WriteStartDocument cannot be called on writers created with ConformanceLevel.Fragment

我在这里错过了什么?

最佳答案

这个问题有一个解决方法。如果在使用序列化程序之前使用了 xml 编写器,则不会写入 header 。以下确实有效,但会在 xml 文件的第一行添加一个空的注释标记

按照 oleksa 的建议改进了代码

static void Main(string[] args)
    {
        Test t1 = new Test(1, "t1", new[] { "a", "b" });
        Test t2 = new Test(2, "t2", new[] { "c", "d", "e" });

        XmlSerializer serializer = new XmlSerializer(typeof(Test));
        //using (StreamWriter writer = new StreamWriter(@"f:\test\test.xml"))
        {
            XmlWriter xmlWriter = XmlWriter.Create(@"test.xml",
                                                   new XmlWriterSettings()
                                                   {
                                                       ConformanceLevel = ConformanceLevel.Fragment,
                                                       OmitXmlDeclaration = false,
                                                       Indent = true,
                                                       NamespaceHandling = NamespaceHandling.OmitDuplicates
                                                   });
            xmlWriter.WriteWhitespace("");
            serializer.Serialize(xmlWriter, t1);
            serializer.Serialize(xmlWriter, t2);
            xmlWriter.Close();
        }
    }

关于c# - .net XmlSerialize 抛出 "WriteStartDocument cannot be called on writers created with ConformanceLevel.Fragment",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19045921/

相关文章:

java - struts 中的 web.xml 以及如何使用 struts-config.xml 配置它

C# Newtonsoft Json JsonProperty - 构造函数 'JsonPropertyAttribute' 具有 0 个参数但使用 1 个参数调用

c# - DataRow 列和字符串值比较

c# - 如何使用 async/await 在 UI 线程上异步运行多个任务?

xml - 在PowerShell中使用msbuild在NuGet中的安装事件上编辑csproj

php - MySQL 和 PHP 与 Google map - 数据源仅在本地工作,无法从 http url 访问时工作

c# - 在或包含查询使用 System.Linq.Dynamic

c# - 尽管连接字符串超时 3 秒,但 SQL 连接等待 15 秒

c# - 如何在ASP.net MVC中显示IIS服务器的自定义错误消息-5

c# - WPF:MVVM - 如果命令为空则禁用按钮