vb.net尝试编写和读取xml

时间:2015-09-04 17:55:44

标签: xml vb.net

我正在尝试写入xml然后阅读它。我可以成功写入,但当我尝试阅读时,我收到以下错误:

未处理的类型' System.Xml.XmlException'发生在System.Xml.dll

其他信息:根级别的数据无效。第1行,第1位。

这是xml文件

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

这是我的代码:

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

xml文件不一定是特别的,我只是用它来关闭存储变量,并在打开时读取变量。

1 个答案:

答案 0 :(得分:0)

试试xml Linq

&#13;
&#13;
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
​
&#13;
&#13;
&#13;