dwb
dwb

Reputation: 483

XmlTextReader error - name cannot begin with the '<' character

So I am getting an error when trying to read an XML document. I have used code like this before with not issues. I have compared to other code and it is all the same so I have basically no clue what the issue is.

Error:

Name cannot begin with the '<' character, hexadecimal value 0x3C. Line 7, position 1.

Full Code:

Imports System.Xml

 Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
        IO.File.WriteAllText(IO.Path.Combine(IO.Path.GetTempPath, "IpadCode.xml"), My.Resources.IpadCode)
    Catch
        MsgBox("Error writing IpadCode list")
    End Try
End Sub

Private Sub GoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click
    Dim store As String = Storenumber.Text
    If (IO.File.Exists("IpadCode.xml")) Then
        Dim document As XmlReader = New XmlTextReader("IpadCode.xml")
        While document.Read()
            If (document.Name = store) Then
                Output.Text = (document.ReadInnerXml)
            End If
        End While
    End If

End Sub
End Class

Sample of XML Document:

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

<Settings>

<50>    123456  </50>
<51>    123457  </51>
<52>    123458  </52>
<53>    123459  </53>
<54>    123460  </54>
<55>    123461  </55>
<56>    123462  </56>
<57>    123463  </57>
<58>    123464  </58>
<59>    123465  </59>
<60>    123466  </60>
</Settings>

Upvotes: 1

Views: 673

Answers (2)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

If your input document really has lines like

<50>    123456  </50>

then it is not well-formed XML. The names of elements cannot start with a number. The first of those elements is not on line 7 though.

If you are the author of this document, change the element names so that the number does not come first in the name:

<e50>    123456  </e50>

or, even better, reconsider whether the elements names should be numbers. The position of elements is easily recoverable from the structure of an XML document, and it's not something you need to represent in element names. Also, every single element will have a different name, which is bad practice and makes it hard to access the content. Usually, elements with the same semantics have the same name.

Upvotes: 3

PhillyNJ
PhillyNJ

Reputation: 3903

XML Elements can not be just a number. This is not a qualified name. An element name must begin with a letter (alphabet) and can be followed by alphanumeric characters.

Upvotes: 4

Related Questions