3

考虑以下 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 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 摆脱它?

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

4

1 回答 1

3

您可以为 XmlTextWriter 派生一个类并覆盖 WriteDocType 方法

它看起来像这样:

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();
于 2012-08-03T15:26:10.433 回答