1

I am trying to decode a custom xml config file in C#, but I am having troubles to create this file from the string I was able to get after my decode step.

After trying to build the xml, I got this error:

System.Xml.XmlException : 'Name cannot begin with the '0' character, hexadecimal value 0x30. Line 1, position 2.'

I know my xml is not a valid xml file because of its bad formatting but I would like to know is there is a way to build it anyways.

Format of the "xml file" :

<01_config.xml>
  <name dataType="String">some_name</name>
  <description dataType="String">some_description</description>
</01_config.xml>

If I replace 01_config.xml by config during debug, everything will work fine since it will become a valid xml file. But it will not be the good format for my config.

I guess I can still build the file without using the C# Xml building tools, but I would like to know if it's possible to do it with it in the first place.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Synops
  • 122
  • 3
  • 11
  • Can you share the code which throws the exception? – Chetan Jan 01 '22 at 12:24
  • `XmlDocument xDoc = new XmlDocument(); xDoc.LoadXml(xml_string);` With xml_string being the string with the decoded xml content – Synops Jan 01 '22 at 13:18
  • 1
    You might consider putting that filename in an attribute and do something like this: `` – Mads Hansen Jan 01 '22 at 15:11
  • I need to keep the same format as it is. That's the whole problem. One way would be to create a real xml file and then edit it after. But it's a bit ugly and I was wondering if there are clearer ways to do it. – Synops Jan 01 '22 at 15:21
  • No, the whole problem is that you refuse to accept what multiple parties have tried to explain to you: Your ask is based on a fundamental flaw: You do not have XML, yet you want to use XML tools/techniques. Either (1) fix your data to be XML, or (2) forgo XML tools/techniques and write a custom parser/processing library for your custom language. You can't have it both ways. You tagged your question both `xml` and `xml-parsing`. Remove those and post a text-level C# code attempt if you opt for #2; they do not apply if you reject #1. – kjhughes Jan 01 '22 at 16:16
  • Yeah, that's what I think too. Without crafting my own parser lib, what I am trying to achieve isn't possible. (also, I am not refusing to accept anything, I think you missundertood my needs. I am naming those conf files as xml because they are very close in syntax and have the XML extension, but yeah, they are not real XML files, so if I had to rephrase my problem : is there a way to create a file with my format using XML tools by, I guess, bypassing some exceptions or else) – Synops Jan 01 '22 at 16:24
  • You've received the answer to that question multiple times already. See duplicate link for options for parsing bad "XML". – kjhughes Jan 01 '22 at 16:26

2 Answers2

2

XML component (element and attribute) names may not begin with a number.

Strictly speaking, this is a matter of the rules for being an XML document – well-formedness, not validity.

Reasons to correct this mistake

  1. You want your document to be XML.
  2. You want users of your document to be able to use the XML ecosystem of editors, parsers, validators, databases, transformation/selection languages, and libraries available in many languages.
  3. You want the interoperability benefits of using a standard.
  4. You want to participate in a community of users – tapping into, and contribute to, a collective body of knowledge to the community's mutual benefit.

Reasons to proceed with bad "XML"

  1. You like the aesthetics of your "XML" variant because it's a "good format for my config".

Recommendation

Fix the mistake and work with standard XML.

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I fully understand that, but those config files are used like this in another program which understand them. So using a standard XML isn't an option for me, sadly. – Synops Jan 01 '22 at 15:24
  • What other program made a mistake that you're looking to perpetuate? – kjhughes Jan 01 '22 at 15:55
  • It's an old private software, but as I said, that's not the point there. I just want to convert this string into a custom XML file. If I can't by using C# libs, I guess I'll have to build one myself. It's not about perpetuating stuff, if I had other options, I would gladly take those :) – Synops Jan 01 '22 at 16:03
  • Then you've reached an [impasse](https://stackoverflow.com/questions/70548526/building-not-valid-xml-document-name-cannot-begin-with-the-0-character/70549594?noredirect=1#comment124712813_70548526) – kjhughes Jan 01 '22 at 16:20
1

I know my xml is not a valid xml file because of its bad formatting but I would like to know is there is a way to build it anyways.

Sure, you can build files that aren't well-formed XML, but then you won't be able to read them using XML tools.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164