C# WriteDocType generates an incorrect PList doctype
-
24-06-2021 - |
Question
Consider the following C# code:
XmlTextWriter writer = new XmlTextWriter(pathFichier, null);
writer.WriteStartDocument();
writer.WriteDocType("plist", "-//Apple//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd", null);
writer.WriteStartElement("plist");
writer.WriteAttributeString("version", "1.0");
writer.WriteFullEndElement(); // plist
writer.Close();
A PList doctype is usually:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
The above code adds an empty subset at the end of the doctype string:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"[]>
The "[]" at the end screws with iOs's parsing.
How can I get rid of it using XmlTextWriter ?
Should I just reopened the file and delete the "[]" ?
Solution
You can derive a class for XmlTextWriter and override the WriteDocType method.
It'd look something like this:
public override void WriteDocType(string name, string pubid, string sysid, string subset)
{
this.wrappedWriter.WriteRaw("-//Apple//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd");
}
EDIT by the guy who asked the question. Here's the code I wrote to make it work.
The WriteDocType override:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace your.namespace {
public class PListWriter : System.Xml.XmlTextWriter {
public PListWriter(string filename, System.Text.Encoding encoding) : base(filename, encoding) { }
public override void WriteDocType(string name = "", string pubid = "", string sysid = "", string subset = "") {
this.WriteRaw("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
}
}
}
Its usage in the wild:
PListWriter writer = new PListWriter(pathFichier, null);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteDocType();
writer.WriteStartElement("plist");
writer.WriteAttributeString("version", "1.0");
writer.WriteFullEndElement(); // plist
writer.Close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow