我正在尝试编辑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
答案 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()
<强>演示:强>