11

While loading XML file in a C# application, I am getting

Name cannot begin with the '1' character, hexadecimal value 0x31. Line 2, position 2.

The XML tag begins like this.

<version="1.0" encoding="us-ascii" standalone="yes" />
<1212041205115912>

I am not supposed to change this tag at any cost.

How can I resolve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
aWebdesigner09
  • 257
  • 2
  • 5
  • 17
  • 1
    If you absolutely can't change the tag then you're going to have to implement your own XML parsing that doesn't validate files as the current tag name won't validate under the XML standard. – Robert Rouhani May 09 '12 at 06:49
  • Thanks. But, I am able to view the same XML with out any validation errors in editors like notepad++. How it is validating? – aWebdesigner09 May 09 '12 at 06:51
  • 3
    Notepad++ by default doesn't validate any files. It just colors them by the location of characters like `<`, `>`, and `"`. – Robert Rouhani May 09 '12 at 06:53
  • thanks.But, I was able to written that file by C# only. Is there any solution? – aWebdesigner09 May 09 '12 at 07:07
  • 1
    @aWebdesigner09 if you use the **proper** tool to generate the XML you can't generate such a file. – Felice Pollano May 09 '12 at 07:12
  • This is a very bad idea. But, it fits into my requirement. I resolved this by - reading xml file as text file by using - replaced numbers by String.Replace(). - then read it again. I am accepting, this is not the correct way. But, C# allows me to write tags like that. – aWebdesigner09 May 09 '12 at 07:34
  • Take a step back, and ask "What does '1211...' mean?" It certainly doesn't *feel* like a tag - it feels like a value. If that's the case, what does the value mean? It is likely something like ``. I understand that you say you cannot change it, but by leaving it, you are setting up for a possible world of hurt later when some other program needs to consume it as XML, which it is not. Take the pain to do it correctly now, make your future maintenance (and those that come after you) a whole lot happier. – Wonko the Sane Nov 30 '18 at 13:49

3 Answers3

16

You are supposed to change the tag name since the one you wrote violates the xml standard. Just to remember the interesting portion of it here:

XML Naming Rules

XML elements MUST follow these naming rules:

  • Names can contain letters, numbers, and other characters
  • Names cannot start with a number or punctuation character
  • Names cannot start with the letters xml (or XML, or Xml, etc)
  • Names cannot contain spaces

Any name can be used, no words are reserved.

as a suggestion to solve your problem mantaining the standard:

  1. Use an attribute, ie <Number value="1212041205115912"/>
  2. Add a prefix to the tag ie <_1212041205115912/>

Of course you can mantain the structure you propose by writing your own format parser, but I can state it would be a really bad idea, because in the future someone would probably extend the format and would not be happy to see that the file that seems xml it is actually not, and he/she can get angry for that. Furthermore, if you want your custom format, use something simpler, I mean: messing a text file with some '<' and '>' does not add any value if it is not an officially recognized format, it is better to use someting like a simple plain text file instead.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
4

IF you absolutely cant change it, eg. for some reason the format is already out in the wild and used by other systems/customers/whatever.

Since it is an invalid xml document, try to clean it up before parsing it. eg. make a regex that replaces all < number> tags with < IMessedUp>number< /IMessedUp> and then parse it.

Sort of iffy way to do it, but I will solve your problem.

Roger Johansson
  • 22,764
  • 18
  • 97
  • 193
4

If you need to process this document, then stop thinking of it as XML, and cast aside any thoughts of using XML tools to process it. You're dealing with a proprietary format and you will need to write your own tools to handle it. If you want the benefits of using XML technology, you will have to redesign your documents so they are valid XML.

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