views:

45

answers:

1

Okay, so this is sort of a hack...but it may have to be. I'm writing an app in XNA, which from my research into this problem apparently doesn't support XML version 1.1. I'm reading in the contents of an ePub document, and one of the newer books contains its content directory as a version 1.1 XML document. This causes my program to crash. However, the structure is the same as the rest, the only thing that should be keeping it from working is the hard-coded "1.0" in the XmlDocument class.

Is it possible that I could read in the file from the stream, see if it contains:

<?xml version="1.1" encoding="UTF-8" standalone="no"?>

and simply replace it with "1.0"? Then I could pull it in as an XmlDocument. I'm not doing any writing to the file, or any complex structural reading, just looking for a few specific nodes, and pulling in the values, so I don't know what the ramifications of this would be.

+2  A: 

You can do this in a very dodgy way by reading the entire XML file into memory and having your way with it:

string content = "";

// Read the XML file into content
StreamReader reader = new StreamReader("file.xml");
content = reader.ReadToEnd();
reader.Close();

// Find the character position just after the <?xml token, and just before the ?> token
int openIndex = content.IndexOf("<?xml", StringComparison.OrdinalIgnoreCase) + 5;
int closeIndex = content.IndexOf("?>", openIndex);

// Get the bits between <?xml and ?>    
string header = content.Substring(openIndex, closeIndex - openIndex);

// Substitute version string.
header = header.Replace("version=\"1.1\"", "version=\"1.0\"");

// Put Humpty Dumpty back together again.
content = string.Concat(content.Substring(0, openIndex), header, content.Substring(closeIndex));

// Feed content into an XMLReader (or equivalent) here.

It works for the example string you provide, but I haven't tested it on imperfectly-formatted XML documents.

Blair Holloway
They should in theory all be perfectly-formatted XML documents...but yeah, that's why it's called a theory. :)Your method works, I may just implement a check to see if it finds 1.1 at line 1, char 16 (that's where it throws the exception), and if it does, then run this processor on it. It's definitely a dodgy way of doing it, I'll make sure I can't find any other way before going about it like this, haha. Thanks for the help!
kcoppock