将信息附加到VB.NEt中的XML文件

时间:2012-11-05 17:46:59

标签: xml vb.net

<Setting1>
   <SubSetting1>Test1</SubSetting1>
   <SubSetting2>Test2</SubSetting2>
</Setting1>
<Setting2>
   <SubSetting1>Test3</SubSetting1>
   <SubSetting2>Test4</SubSetting2>
</Setting2>

这只是一个参考xml文档的小例子。我正在尝试学习如何附加现有的xml文档,而不是写入xml标记。我的实际xml文档有更多节点(大约40个),我将所有新值(代替Test1,Test2,..等)放入列表中。按照正确的顺序,我正在考虑使用For循环来写入值。

这样的事情?

Public Sub example (ListofSettings As List(of String))
    Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory)
    Dim myNodes, myChildren As XmlNodeList
    settingsFile.Load(SETTINGS_FILE_PATH)
    myNodes = settingsFile.GetElementsByTagName("Settings1")
For i as Integer = 0 to ListofSettings.count
For Each n As XmlNode In myNodes
        If n.Name = "Settings1" Then
            myChildren = n.ChildNodes
            For Each n1 As XmlNode In myChildren
                Select Case n1.Name
                    Case "SubSetting1"
                        writer.WriteElementString(**DONT WANT TO CHANGE TAG NAME**, ListofSettings.Item(i))
End Select
Next
End if    
Next

1 个答案:

答案 0 :(得分:0)

使用Xmldocument.SelectSingleNode并更改所需节点的innerXml。有关示例,请参阅MSDN