0

I'm designing a save-data editing application using Windows Forms and C#

The save data is stored in simple un-indexed XML files (savegame.xml)
Everything in my save editor is working, however, these save data files contain a tag called "reward" which is filled with tags that contain numbers as tag names:

<reward>
    <1000 value="1"/>
    <2000 value="1"/>
    <3000 value="1"/>
</reward>

The application that uses this save data has a "Custom" XML parser that supports numbers in tag names. The issue is that the default XmlDocument in windows form's C# does support numbers in tag names (Likewise with pretty much any other XML parser out there)
So, this tiny tag collection at the bottom of the file is crashing the entire application. Is it possible to "ignore" these rules and continue on?

Furthermore, is it possible to use XmlDocument's XmlDocument.CreateElement() to create those same elements with numbers for tag names?

The program requires these (Otherwise it doesn't accept the save file as valid)

The only thing I can think of to get around it, is to simply delete it from the raw text, edit the XML with XmlDocument and then save it, placing the reward tags back in afterwards manually..

Code I use to grab the save data which is causing the error:

var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Teardown\\savegame.xml");
XmlDocument xmlDoc = new XmlDocument();
using (StreamReader xmlSr = new StreamReader(fileName, true))
{
    xmlDoc.Load(xmlSr);
}

Example error from one of my attempts:

An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll
Name cannot begin with the '2' character, hexadecimal value 0x32. Line 283, position 5.
13
  • 2
    That's not XML so you can't use XmlDocument. Try an SGML or lenient-HTML parser like HTML Agility pack. Mar 3, 2021 at 16:58
  • Well, a possible workaround is use Regex to replace \<\d+ by a valid tag, do your processing, and undo the replace.
    – Magnetron
    Mar 3, 2021 at 17:03
  • @Magnetron Could you elaborate? Id rather not switch to that "HTML Agility Pack" package suggested.. Because ive already written a massive program built around XmlDocument. Id appreciate if you could show me how to "undo" the replace after I make edits. Mar 3, 2021 at 17:11
  • @RandomWebDev well, I'm not sure that is the only problem with your file, since <1000 value="1"> is also not closed, so I don't think XmlDocument or XDocument would parse it correctly.
    – Magnetron
    Mar 3, 2021 at 17:13
  • But what I have suggested was using regex to change your input to something like <myfaketag original-tag="1000" value="1"> and then, after processing, use Regex again to revert to the original form.
    – Magnetron
    Mar 3, 2021 at 17:15

0

Browse other questions tagged or ask your own question.