Question

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 encodes its content 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 is 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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top