.NET XMLSERIALIZE抛出" WriteStartDocument不能叫上ConformanceLevel.Fragment&QUOT创建作家;抛出、作家、XMLSERIALIZ

2023-09-05 03:43:51 作者:忘记爱过她

我试图序列化类,编写XML文件为多个片段,即写类作为一个单独的片段,每一个对象,如果没有XML头/根。下面是一个示例code:

Am trying to serialize a class, writing to an XML file as multiple fragments, i.e, write each object of the class as an individual fragment, without the XML header/root. Below is a sample code:

[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();
        }
    }
}

在第一次调用序列化,我得到异常:

In the first call to serialize, I get the exception:

WriteStartDocument cannot be called on writers created with ConformanceLevel.Fragment

我是什么在这里失踪?

What am I missing here?

推荐答案

还有一个解决此问题的。当XML作家已使用您使用序列化之前,则该头将不会被写入。下面做工作,但会在XML文件的第一行

there is a workaround to this problem. When the xml writer has been used before you use the serializer, then the header won't be written. The following does work, but will add an empty comment tag on the first line of the xml file

提高code所建议的oleksa

improved code as suggested by 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();
        }
    }