2

I am working with a large XML file that deals with localization of strings in 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.

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. 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. Specifically  which appears to be cancel. I'm not sure why this is here... the file is about 500000 lines long though.

I manually changed the header before my java parser grabbed it to 1.1, and it worked. Technically I could modify the XML as a string and then parse that, but I'd rather have proper XML instead.

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#.

Is there a trick to this?

Tomalak
  • 332,285
  • 67
  • 532
  • 628
Mgamerz
  • 2,872
  • 2
  • 27
  • 48
  • 1
    How are you specifying the content to write? e.g. `WriteString`? [`WriteString`](http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.writestring(v=vs.110).aspx) has a few remarks about invalid characters. – ta.speot.is Dec 05 '14 at 07:30
  • XmlTextWriter internally uses the [`XmlCharType`](http://referencesource.microsoft.com/#System.Xml/System/Xml/XmlCharType.cs) when writing text data to the document. That type is confined to characters that are valid in XML 1.0. I very much doubt that you can even write `\x18` to your XML document using normal means. – Tomalak Dec 05 '14 at 07:44
  • The content that is being written is a decompiled localization file (Talkfile, TLK) that is used in unreal engine games. I can get my java to parse and write it if I change the header to use 1.1 manually. I looked around the documentation some more, and I don't think .NET even supports 1.l, which sucks. – Mgamerz Dec 05 '14 at 07:56

2 Answers2

1

Use the following process:

  • Create a custom implementation of the IXmlSerializable interface
  • Handle  using a custom method
  • Use an instance of the implementation to read the document

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
0

Can you replace WriteStartDocument() with WriteRaw("<?xml version=\"1.1\"?>") and remove WriteEndDocument()?

Andy Bradbrook
  • 355
  • 3
  • 6