简体   繁体   English

C# XMLTextWriter - XML 1.1

[英]C# XMLTextWriter - XML 1.1

I am working with a large XML file that deals with localization of strings in C#.我正在处理一个大型 XML 文件,该文件处理 C# 中字符串的本地化。 I did not write the program that produces the XML, but I have access to the source and I want to modify it, as it currently outputs 1.0 as the version, but it contains illegal characters in the document.我没有编写生成 XML 的程序,但我可以访问源代码并且我想修改它,因为它当前输出 1.0 作为版本,但它在文档中包含非法字符。

Here is how the code is currently written out.以下是代码当前的编写方式。

XmlTextWriter xr = new XmlTextWriter(fileName, Encoding.UTF8);
xr.Formatting = Formatting.Indented; 
xr.Indentation = 4;
xr.WriteStartDocument();
xr.WriteStartElement("tlkFile");
// ...

However the WriteStartDocument() documentation states that it uses XML version 1.0.但是, WriteStartDocument()文档声明它使用 XML 1.0 版。 I know this does not work, because I am writing a java parser that modifies this XML, and it can't load because it has unicode characters that aren't allowed.我知道这不起作用,因为我正在编写一个修改此 XML 的 java 解析器,它无法加载,因为它包含不允许的 unicode 字符。 Specifically 特别是 which appears to be cancel.这似乎是取消。 I'm not sure why this is here... the file is about 500000 lines long though.我不知道为什么会在这里……不过文件大约有 500000 行。

I manually changed the header before my java parser grabbed it to 1.1, and it worked.在我的 java 解析器将它抓取到 1.1 之前,我手动更改了标头,并且它起作用了。 Technically I could modify the XML as a string and then parse that, but I'd rather have proper XML instead.从技术上讲,我可以将 XML 修改为字符串,然后对其进行解析,但我宁愿使用适当的 XML。

I cannot find any way to change this.我找不到任何方法来改变这一点。 I can't really change the tool to use some other form of XML writing because it is a shared open source project that is used, and I'm not that great with C#.我无法真正更改工具以使用其他形式的 XML 编写,因为它是一个使用的共享开源项目,而且我不太擅长 C#。

Is there a trick to this?这有什么窍门吗?

Use the following process:使用以下过程:

  • Create a custom implementation of the IXmlSerializable interface创建 IXmlSerializable 接口的自定义实现
  • Handle 处理 using a custom method使用自定义方法
  • Use an instance of the implementation to read the document使用实现的实例来读取文档

References参考

你能用 WriteRaw("<?xml version=\\"1.1\\"?>") 替换 WriteStartDocument() 并删除 WriteEndDocument() 吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM