0

I am trying to write to xml then read it. I can successfully write but when I try to read it I get the following error:

An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll

Additional information: Data at the root level is invalid. Line 1, position 1.

here is the xml file

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Settings>
  <Variables>
    <var_name>varsender</var_name>
    <value>name@email.com</value>
  </Variables>
</Settings>

here is my code:

Imports System.Xml

Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim writer As New XmlTextWriter(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Scripts\Test\testxml.xml", System.Text.Encoding.UTF8)
        createNode("varsender", "name@email.com", writer)
    End Sub


    Private Sub createNode(ByVal varname As String, ByVal varvalue As String, ByVal writer As XmlTextWriter)
        Dim settings As XmlWriterSettings = New XmlWriterSettings()
        settings.OmitXmlDeclaration = True
        settings.ConformanceLevel = ConformanceLevel.Fragment
        settings.CloseOutput = False

        writer.WriteStartDocument(True)
        writer.Formatting = Formatting.Indented
        writer.Indentation = 2
        writer.WriteStartElement("Settings") 'name of first element
        writer.WriteStartElement("Variables")
        writer.WriteStartElement("var_name")
        writer.WriteString(varname)
        writer.WriteEndElement()
        writer.WriteStartElement("value")
        writer.WriteString(varvalue)
        writer.WriteEndElement()
        writer.WriteEndElement()


        writer.WriteEndElement()
        writer.WriteEndDocument()
        writer.Close()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim doc As XmlDocument = New XmlDocument()
        doc.LoadXml(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Scripts\Test\testxml.xml")

        Dim variable As String = doc.SelectSingleNode("/Settings/Variables[var_name='" & TextBox1.Text & "']/value") _
                             .InnerText


        TextBox2.Text = variable

    End Sub
End Class

the xml file doesn't have to be anything special, I am just going to be using it to store variables at close, and read the variables on open.

John
  • 55
  • 5
  • http://stackoverflow.com/questions/7544475/data-at-the-root-level-is-invalid-line-1-position-1-why-do-i-get-this-error-w You need to use `Load`, not `LoadXml` – Paul Abbott Sep 04 '15 at 18:10

1 Answers1

0

Try xml Linq

Imports System.Xml
Imports System.Xml.Linq
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) & "\Scripts\Test\testxml.xml"
        Dim doc As XDocument = Nothing
        createNode("varsender", "name@email.com", doc)
        doc.Save(filename)
    End Sub
    Private Sub createNode(ByVal varname As String, ByVal varvalue As String, ByRef doc As XDocument)
        Dim identification As String = "<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?><Settings></Settings>"
        doc = XDocument.Parse(identification)
        Dim settings As XElement = doc.FirstNode

        Dim variables As New XElement("Variables", New Object() {
                                      New XElement("var_name", varname),
                                      New XElement("value", varvalue)
                                      })
        settings.Add(variables)

    End Sub


End Class
​
jdweng
  • 33,250
  • 2
  • 15
  • 20