XmlTextReader 错误 - 名称不能以“<”字符开头



所以我在尝试读取XML文档时遇到错误。我以前使用过这样的代码,没有问题。我已经与其他代码进行了比较,它都是一样的,所以我基本上不知道问题是什么。

错误:

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

完整代码:

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

XML 文档示例:

    <?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>

XML 元素不能只是一个数字。这不是限定名称。 元素名称必须以字母(字母)开头,后跟字母数字字符。

如果您的输入文档确实有类似

<50>    123456  </50>

那么它就不是格式正确的 XML。元素的名称不能以数字开头。不过,这些元素中的第一个不在第 7 行。

如果您是本文档的作者,请更改元素名称,使数字不在名称的前面:

<e50>    123456  </e50>

或者,更好的是,重新考虑元素名称是否应该是数字。元素的位置很容易从 XML 文档的结构中恢复,并且不需要在元素名称中表示。此外,每个元素都有不同的名称,这是不好的做法,很难访问内容。通常,具有相同语义的元素具有相同的名称。

最新更新