将信息附加到 VB.NET 中的 XML 文件 [英] Appending information to an XML file in VB.NEt

查看:20
本文介绍了将信息附加到 VB.NET 中的 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

This is just a small example for reference to an xml document. I am trying to learn how to append an existing xml document, with out writing over the xml tag. My actual xml document has more nodes (about 40) and I am putting all of the new values (in place of Test1, Test2, ..etc) into a list. In the correct order, So I was thinking using a For loop to just write the value.

类似的东西?

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

推荐答案

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

Use Xmldocument.SelectSingleNode and change the innerXml of the node that you want. See MSDN for an example

这篇关于将信息附加到 VB.NET 中的 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆