.net XmlSerialize бросает «WriteStartDocument не может быть вызван на устройствах записи, созданных с помощью ConformanceLevel.Fragment»
Пытаюсь сериализовать класс, записывая в файл XML как несколько фрагментов, т.е. записывать каждый объект класса как отдельный фрагмент, без заголовка / корня XML. Ниже приведен пример кода:
[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
Что мне здесь не хватает?