Name cannot begin with the ' ' character

106,467

Solution 1

Yes, you should delete the space between <? and xml.

<?xml version="1.0" encoding="utf-8" ?>
<form>
   <e order="0" type="custom" name="test">
      <fi type="text" />
      <o />
   </e>
   <e order="1" type="zip" />
   <e order="2" type="state" />
</form>

Here's the relevant XML spec.

Solution 2

Another common source of this error is when the XmlReader attempts to read your scripts as xml. That's a good reason to start commenting out scripts after the script tags. They will still run:

<script language="javascript" type="text/javascript">
<!--
    function myFunction() {
    }
    ...
-->
</script>

Solution 3

Your error message is quite explicit, you have an error at posn 3 in line 1. Try <?xml -- no space.

Solution 4

Remove the first space in the document:

<?xml version="1.0" encoding="utf-8"?>

Solution 5

My error in same case was that file wasn't saved as UTF-8.

Share:
106,467
Brian Hicks
Author by

Brian Hicks

Updated on July 09, 2022

Comments

  • Brian Hicks
    Brian Hicks almost 2 years

    I'm parsing some XML in C#. I'm getting it from a database, and so converting it to a MemoryStream before reading it with an XmlTextReader. The problem is that I get this error: Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 1, position 3. Following is my XML and my code for reading it (it's coming out of the database alright, no blank first character). Any suggestions?

    XML:

    <? xml version="1.0" encoding="utf-8" ?>
    <form>
       <e order="0" type="custom" name="test">
          <fi type="text" />
          <o />
       </e>
       <e order="1" type="zip" />
       <e order="2" type="state" />
    </form>
    

    C#:

    byte[] byteArray = new byte[formXml.Length];
    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    byteArray = encoding.GetBytes(formXml);
    MemoryStream xmlStream = new MemoryStream(byteArray);
    
    XmlTextReader xmlReader = new XmlTextReader(xmlStream);
    while (xmlReader.Read())
    {
        if (xmlReader.HasValue)
        {
            returnString += xmlReader.Depth.ToString();
        }
    }
    

    I thought it could be the encoding, but I've tried by UTF8 and ASCII and can't find anything.

  • AakashM
    AakashM about 14 years
    And the error message is (for once) completely spot on about the location of the problem.