如何使用组合框编辑XML节点(VB.Net)

时间:2013-08-30 13:50:41

标签: xml vb.net

我正在尝试编辑Income节点,并使用当前代码实现此目的。但是,正在删除所有其他元素(RptMenu和Icon)。我希望它们保留在XML文件中,只有收入才能更改为文本框设置的内容。组合框用于选择要更改的MenuItem(收入,股票等)。

XML代码:(不包含更多菜单项以保持简单)

<ReportMenu>

<RptMenu category="Menu">
    <MenuItem>Income</MenuItem>
    <Icon>C:\Documents\Visual Studio 2008\Projects\Menu\Menu\Resources\IncomeImage.jpg</Icon>
  </RptMenu>

<RptMenu category="Menu">
    <MenuItem>Stock</MenuItem>
    <Icon>C:\Documents\Visual Studio 2008\Projects\Menu\Menu\Resources\IncomeImage.jpg</Icon>
  </RptMenu>

 </ReportMenu>

vb.net代码:

Private Sub btnEditCategory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditCategory.Click

Dim xd As New XmlDocument()
        xd.Load("C:\Documents\Reports.xml")

        Dim newNode As XmlElement = xd.CreateElement("MenuItem")
        newNode.InnerText = txtAddCategory.Text

        For Each oldNode As XmlNode In xd.SelectNodes("ReportMenu/RptMenu")

            If oldNode.SelectSingleNode("MenuItem").InnerText = cmbCategory.Text Then
                oldNode.ParentNode.ReplaceChild(newNode, oldNode)
            End If

        Next
        xd.Save("C:\Documents\Reports.xml")

    End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<Profiles>
<Name>
</Name>
</Profiles>

Dim doc As XDocument = XDocument.Load("Profiles.xml")
ComboBox1.DataSource = (From element In doc.Descendants("Name") Select element.Value).ToList()

<强>演示:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/1faf99bd-f22e-4f52-aa0a-2d4328835537/c-change-xml-node