【问题标题】:VB.NET, XML, Write and Save with inner text and attributesVB.NET、XML、写入和保存,带有内部文本和属性
【发布时间】:2020-11-12 08:54:03
【问题描述】:

我有一个基本程序可以在我的家用计算机上自动执行一些操作。我在 VB.NET 中使用 Visual Studio Community edition 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>

在此先谢谢你了:)

【问题讨论】:

  • 您可以将属性作为子节点附加到节点,并将文本值作为子节点附加。该值本身就是一个节点。例如Dim nodeValue = xDoc.CreateTextNode("txt_xml_title.text")nodeTitle.AppendChild(nodeValue)
  • 您这样做是为了存储设置吗?已经有一个内置机制可以做到这一点,你看..
  • 克雷格 - 我会用你的建议代替什么?我试过只是将它添加到混合物中,但这并没有解决任何问题。 . Caius - 它不仅用于设置,XML 文件中还有其他内容,我更喜欢可以将 xml 文件移动到 PC 上任何位置的东西(如果我想保持便携,还可以使用记忆笔)
  • 您可以将分配替换为节点内部文本。我遗漏了执行属性的正确方法,有一个名为 SetAttribute 的节点成员可以用于此,例如nodeTitle.SetAttribute("location", "txt_xml_location.text").
  • 除此之外,我不清楚是什么阻碍了你/你的问题在哪里。可能,您实际上并不希望 txt.... 文本项周围有引号,因为您想使用该文本值。

标签: xml vb.net visual-studio attributes innertext


【解决方案1】:

根据你的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")

【讨论】:

  • 你先生,太棒了。这非常有效。我也没有那么远。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 2014-12-18
  • 1970-01-01
  • 2015-06-12
  • 2011-07-24
相关资源
最近更新 更多