简体   繁体   English

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

[英]How to edit XML node using combobox (VB.Net)

I'm trying to edit the Income node, and am achieving this with the current code. 我正在尝试编辑收入节点,并使用当前代码实现了这一点。 However all other elements (RptMenu and Icon) are being removed. 但是,所有其他元素(RptMenu和Icon)都将被删除。 I want them to remain in the XML file and only Income to be changed to whatever the textbox sets. 我希望它们保留在XML文件中,并且只将Income更改为文本框设置的内容。 A combobox is used to select which MenuItem (Income, Stock etc) is to be changed. 组合框用于选择要更改的MenuItem(收入,库存等)。

XML Code: (There are more menu items not included to keep it simple) 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 Code: 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

Try this: 尝试这个:

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

OR 要么

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

Demo: 演示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM