转换包含XDocumentType的XDocument会导致NotSupportedException

时间:2012-06-07 14:02:35

标签: windows-phone-7 linq-to-xml xbel

我正在尝试在包含以下文档类型的WP7.1.1上创建和导出XDocument:

<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange
    Language 1.0//EN//XML"
    "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">

不幸的是,到目前为止我在所有尝试中都遇到了NotSupportedExceptions,我不知道该如何离开这里。以下是我尝试过的一些摘录:

/* create document */
var document = new XDocument();
var doctype = new XDocumentType("xbel", null, null, null); 
document.AddFirst(doctype); // << everything working without this line

/* document header */
var version = new XAttribute("version", "1.0");
var root = new XElement("xbel", version);
document.Add(root);

/* convert to string1 */
var text1 = document.ToString(); // << NotSupportedException was unhandled

/* convert to string2 */
var stringBuilder = new StringBuilder();
var stringWriter = new StringWriter(stringBuilder);
document.Save(stringWriter); // << NotSupportedException was unhandled
var text2 = stringBuilder.ToString();

此问题可能与thisthis问题有关。

1 个答案:

答案 0 :(得分:2)

对于我的具体问题,这是一个hacky解决方案:

/* format */ 
var stringBuilder = new StringBuilder();
var stringWriter = new StringWriter(stringBuilder); 
document.Save(stringWriter);
var text = stringBuilder.ToString();

/* document type */
const string subset = "<!DOCTYPE xbel PUBLIC \"+//IDN python.org//DTD XML Bookmark Exchange Language 1.0" +
    "//EN//XML\" \"http://www.python.org/topics/xml/dtds/xbel-1.0.dtd\">";
return text.Replace("?>", "?>" + Environment.NewLine + subset); 

当框架的缺点和错误迫使你编写像这样的奇怪代码时,总是很难过,但是当这些遭遇与Windows Phone一样频繁时更是如此。