VB.NET、XML、写入并保存内部文本和属性

VB.NET, XML, Write and Save with inner text and attributes

我有一个基本程序可以在我的家用计算机上自动执行一些操作。我在 VB.NET

中使用 Visual Studio 社区版 2019

我的 xml 与此相似

<data>
    <settings topbar="gray"></settings>
    <settings bottombar="gray"></settings>
    <settings font="arial"></settings>
    <settings otherstuff="moresettings"></settings>
    <backup location="c:\folder1">fave folder</backup>
    <backup location="c:\folder2">not fave folder</backup>
    <backup location="c:\folder3">another folder</backup>
</data>

我已经让其他一切正常。我可以毫无问题地读取设置和执行备份,但我想要一种方法让程序将数据插入 xml 文档,而无需我手动输入。 使用 FolderBrowserDialog() 比输入错误更少。

我的代码的前半部分工作正常,但我也想添加它,以防万一它需要重写才能使其余部分正常工作。这是在一个基本按钮上设置的

If txt_xml_title.Text = "" Then
            MessageBox.Show("Title cannot be blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        If txt_xml_folder.Text = "" Then
            MessageBox.Show("Location not specified", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Dim xDoc As New XmlDocument()
        xDoc.Load(Application.StartupPath & "/xml.xml")
        Dim nodes As XmlNodeList = xDoc.DocumentElement.SelectNodes("/data/backup")
        For Each node As XmlNode In nodes
            If node.InnerText = txt_xml_title.Text Then
                MessageBox.Show(txt_xml_title.Text & " is already declared, please rename the item", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
        Next

现在这是我摸索了很多次并重写了很多次的地方。 (老实说,我真的什么都不懂了。我在这里看到了一些 C# 的答案,但这完全超出了我的范围。我只是一个 VB.NET 菜鸟 :)


        Dim nodeTitle As XmlNode = xDoc.CreateElement("backup")
        Dim nodeAtribute As XmlNode = xDoc.CreateAttribute("location")

        xDoc.CreateNode(XmlNodeType.Element, "backup", "location")
        nodeTitle.InnerText = txt_xml_title.Text
        nodeAtribute.InnerText = txt_xml_folder.Text
        xDoc.AppendChild(nodeTitle)
        xDoc.AppendChild(nodeAtribute)


        xDoc.Save(Application.StartupPath & "/xml.xml")

如果我找错树了,我怎么能添加一个新行来创建

<backup location="txt_xml_location.text">txt_xml_title.text</backup>

提前谢谢菜鸟:)

根据您的 xml 文件,您需要在根元素下添加子元素,因此使用 XmlDocument.DocumentElement Property 获取文档的根 XmlElement:

Dim nodeTitle As XmlElement = xDoc.CreateElement("backup")
'...
xDoc.DocumentElement.AppendChild(nodeTitle)

整个代码如下:

    If txt_xml_title.Text = "" Then
        MessageBox.Show("Title cannot be blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If
    If txt_xml_folder.Text = "" Then
        MessageBox.Show("Location not specified", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If

    Dim xDoc As New XmlDocument()
    xDoc.Load(Application.StartupPath & "/xml.xml")
    Dim nodes As XmlNodeList = xDoc.DocumentElement.SelectNodes("/data/backup")
    For Each node As XmlNode In nodes
        If node.InnerText = txt_xml_title.Text Then
            MessageBox.Show(txt_xml_title.Text & " is already declared, please rename the item", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
    Next

    Dim nodeTitle As XmlElement = xDoc.CreateElement("backup")
    nodeTitle.SetAttribute("location", txt_xml_folder.Text)
    nodeTitle.InnerText = txt_xml_title.Text
    xDoc.DocumentElement.AppendChild(nodeTitle)

    xDoc.Save(Application.StartupPath & "/xml.xml")