Ultracoustic
Ultracoustic

Reputation: 309

Cannot alter settings of XmlWriter

I am having some trouble with the XmlWriter class. I create and instantiate it using the XmlWriter.Create() method, but when I do it becomes a type XmlWellFormedWriter. I want to be able to write XML fragments to a file but when I try to do so I encounter an exception which tells me to set the Conformance Level to Auto or Fragment. I have tried to change the settings after instantiating the object:

XmlWriter writer = XmlWriter.Create(filepath);
writer.settings.ConformanceLevel = ConformanceLevel.Auto;

But I encounter a exception saying that the Conformance Level is read-only and cannot be set.

I have also tried creating settings and having my XmlWriter inherhit them:

XmlWriterSettings settings = new XmlWriterSettings();
settings.ConformanceLevel = ConformanceLevel.Auto;
XmlWriter writer = XmlWriter.Create(filepath, settings);

But the created XmlWriter instance overrides my settings with its own, setting the Conformance level to Document. How can I fix this? I am using this website as a tutorial.

Upvotes: 3

Views: 1830

Answers (1)

Sievajet
Sievajet

Reputation: 3513

The XmlWriter behaves like it should. ConformanceLevel.Auto specifies that the XML writer should determine the level of conformance checking based on the incoming data. This setting can be useful when you don't know whether the generated XML will be a well-formed XML document or a fragment. In your case its switching to ConformanceLevel.Document. The ConformanceLevel property can be used to check for specifc incoming data, ConformanceLevel.Document or ConformanceLevel.Fragment.

Upvotes: 1

Related Questions