UnclePaul
UnclePaul

Reputation: 515

Converting XDocument containing XDocumentType causes a NotSupportedException

I'm trying to create and export a XDocument on WP7.1.1 containing the following document type:

<!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">

Unfortunately I was greeted with NotSupportedExceptions in all my attempts so far and I don't know how to go from here. Here is a little excerpt of things I tried:

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

This problem might be related to this and this question.

Upvotes: 1

Views: 214

Answers (1)

UnclePaul
UnclePaul

Reputation: 515

Here is a hacky solution for my specific problem:

/* 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); 

It's always sad when the shortcomings and bugs of a framework force you to write strange code like this, but it's even worse when those encounters are as frequent as they are with Windows Phone.

Upvotes: 2

Related Questions