C#WriteDocType生成错误的PList doctype

时间:2012-08-03 15:20:10

标签: c# plist doctype xmltextwriter

考虑以下C#代码:

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

PList doctype通常是:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

上面的代码在doctype字符串的末尾添加了一个空子集:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"[]>

结尾处的“[]”用iOs解析。

如何使用XmlTextWriter摆脱它?

我应该重新打开文件并删除“[]”吗?

1 个答案:

答案 0 :(得分:3)

您可以为XmlTextWriter和override the WriteDocType method派生一个类。

它看起来像这样:

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");
}

由提出问题的人编辑。这是我为使其工作而编写的代码。

WriteDocType覆盖:

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\">");
        }
    }
}

它在野外使用:

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