通过 XNA 中的 StreamReader 替换 XML 文件中的单词?

发布于 2024-09-02 23:42:35 字数 396 浏览 2 评论 0原文

好吧,这有点像黑客攻击……但可能必须如此。我正在 XNA 中编写一个应用程序,根据我对这个问题的研究,它显然不支持 XML 版本 1.1。我正在阅读 ePub 文档的内容,其中一本较新的书籍将其内容编码为 1.1 版 XML 文档。这导致我的程序崩溃,但是结构与其余的相同。唯一阻止它工作的是 XmlDocument 类中的硬编码“1.0”。

我是否可以从流中读取文件,看看它是否包含:

并简单地将其替换为“1.0”?然后我可以将其作为 XmlDocument 拉入。我没有对文件进行任何写入,也没有进行任何复杂的结构读取,只是寻找一些特定的节点并提取值,所以我不知道这会产生什么后果。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-09-09 23:42:35

您可以通过将整个 XML 文件读入内存并按照自己的方式使用它,以一种非常狡猾的方式完成此操作:

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.

它适用于您提供的示例字符串,但我还没有在格式不完美的 XML 文档上测试它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文