我似乎无法使用XmlDocument方法在另一个节点之后添加节点。我已经尝试了XMLdocument
的方法,但他们似乎没有给出
我需要的是什么。
现有的xml文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<FileZillaServer>
<Groups/>
<Users>
<User Name="dbrown">
<Option Name="Pass">2ac9cb7dc02b3c0083eb70898e549b63</Option>
<IpFilter>
<Disallowed/>
<Allowed/>
</IpFilter>
<Permissions>
<Permission Dir="C:\inetpub\wwwroot">
<Option Name="FileRead">1</Option>
</Permission>
</Permissions>
<SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="0" UlType="0" UlLimit="10" ServerUlLimitBypass="0">
<Download/>
<Upload/>
</SpeedLimits>
</User>
</Users>
<User Name="3">
<Option Name="Pass"/>
<IPFilter>
<Disallowed/>
<Allowed/>
</IPFilter>
<Permissions>
<Permission Dir="C:\inetpub\wwwroot">
<Option Name="FileRead"/>
</Permission>
</Permissions>
<SpeedLimits DType="0" DLimit="10" ServerDLimitBypass="0" UType="0" ULimit="10" ServerULimitBypass="0">
<Download/>
<Upload/>
</SpeedLimits>
<PrivateFtpAccountId-3>55555</PrivateFtpAccountId-3>
<PrivatePassword-3>test5</PrivatePassword-3>
<PublicFtpAccountId-3>66666</PublicFtpAccountId-3>
<PublicPassword-3>test6</PublicPassword-3>
</User>
</FileZillaServer>
我运行了我的控制台应用程序代码以在下面添加此新节点部分,但我想在第一个用户组之后添加它----&gt; ....
<User Name="3">
<Option Name="Pass"/>
<IPFilter>
<Disallowed/>
<Allowed/>
</IPFilter>
<Permissions>
<Permission Dir="C:\inetpub\wwwroot">
<Option Name="FileRead"/>
</Permission>
</Permissions>
<SpeedLimits DType="0" DLimit="10" ServerDLimitBypass="0" UType="0" ULimit="10" ServerULimitBypass="0">
<Download/>
<Upload/>
</SpeedLimits>
<PrivateFtpAccountId-3>55555</PrivateFtpAccountId-3>
<PrivatePassword-3>test5</PrivatePassword-3>
<PublicFtpAccountId-3>66666</PublicFtpAccountId-3>
<PublicPassword-3>test6</PublicPassword-3>
</User>
似乎无法让它发挥作用。
我的控制台应用代码是:
Dim xmleUserNameRoot As XmlElement
Dim xmleOptionElement10 As XmlElement
Dim xmleOptionElement20 As XmlElement
Dim xmleOptionElement21 As XmlElement
Dim xmleOptionElement22 As XmlElement
Dim xmleOptionElement23 As XmlElement
Dim xmleIpFilterRoot As XmlElement
Dim xmlePermissionsGroup As XmlElement
Dim xmlePermissionRoot As XmlElement
Dim xmleSpeedLimits As XmlElement
Dim xmleElementPrivateFtpAcctId As XmlElement
Dim xmleElementPrivatePassword As XmlElement
Dim xmleElementPublicFtpAcctId As XmlElement
Dim xmleElementPublicPassword As XmlElement
Dim count As Integer
Dim strId As String
Dim strPrivateFtpAcctId As String
Dim strPrivatePassword As String
Dim strPublicFtpAcctId As String
Dim strPublicPassword As String
strPrivateFtpAcctId = "55555"
strPrivatePassword = "test5"
strPublicFtpAcctId = "66666"
strPublicPassword = "test6"
' Can be any Id.
strId = "3"
' Create a new XmlDocument class, and use the Load method to load the XML file.
Dim myXmlDocument As New XmlDocument()
' The XmlDocument class represents the XML document and has a Load method to load the document from a file, stream, or an XmlReader.
' So load in the XML file.
myXmlDocument.Load("MyFiLeZillaforadding.xml")
' For starting User Name.
xmleUserNameRoot = myXmlDocument.CreateElement("User")
' Create an attribute and set its value to that of the new id.
xmleUserNameRoot.SetAttribute("Name", strId)
myXmlDocument.DocumentElement.AppendChild(xmleUserNameRoot)
' Create a new element with an attribute and add it in to the User group.
xmleOptionElement1 = myXmlDocument.CreateElement("Option")
xmleOptionElement1.SetAttribute("Name", "Pass")
xmleUserNameRoot.AppendChild(xmleOptionElement1)
' Create the new "IPFilter" root.
xmleIpFilterRoot = myXmlDocument.CreateElement("IPFilter")
' Add elements to the "IPFilter" root.
xmleOptionElement20 = myXmlDocument.CreateElement("Disallowed")
xmleIpFilterRoot.AppendChild(xmleOptionElement20)
xmleOptionElement21 = myXmlDocument.CreateElement("Allowed")
xmleIpFilterRoot.AppendChild(xmleOptionElement21)
' Add the "IPFilter" root.
xmleUserNameRoot.AppendChild(xmleIpFilterRoot)
' Create the new "Permissions" group.
xmlePermissionsGroup = myXmlDocument.CreateElement("Permissions")
' Create the new "Permission" root.
xmlePermissionRoot = myXmlDocument.CreateElement("Permission")
xmlePermissionRoot.SetAttribute("Dir", "C:\inetpub\wwwroot")
' Add elements to the "Permission" root.
xmleOptionElement10 = myXmlDocument.CreateElement("Option")
xmleOptionElement10.SetAttribute("Name", "FileRead")
xmlePermissionRoot.AppendChild(xmleOptionElement10)
' Add the "Permission" root to the "Permissions" group.
xmlePermissionsGroup.AppendChild(xmlePermissionRoot)
' Add the "Permissions" group.
xmleUserNameRoot.AppendChild(xmlePermissionsGroup)
' Create the new "SpeedLimits" root.
xmleSpeedLimits = myXmlDocument.CreateElement("SpeedLimits")
xmleSpeedLimits.SetAttribute("ServerULimitBypass", "0")
xmleSpeedLimits.SetAttribute("ULimit", "10")
xmleSpeedLimits.SetAttribute("UType", "0")
xmleSpeedLimits.SetAttribute("ServerDLimitBypass", "0")
xmleSpeedLimits.SetAttribute("DLimit", "10")
xmleSpeedLimits.SetAttribute("DType", "0")
' Add elements to the "SpeedLimits" root.
xmleOptionElement22 = myXmlDocument.CreateElement("Download")
xmleSpeedLimits.AppendChild(xmleOptionElement22)
xmleOptionElement23 = myXmlDocument.CreateElement("Upload")
xmleSpeedLimits.AppendChild(xmleOptionElement23)
' Add the "SpeedLimits" root.
xmleUserNameRoot.AppendChild(xmleSpeedLimits)
' Add the remaining to User Name="#".
xmleElementPrivateFtpAcctId = myXmlDocument.CreateElement("PrivateFtpAccountId-" & strId)
xmleElementPrivateFtpAcctId.InnerText = strPrivateFtpAcctId
xmleUserNameRoot.AppendChild(xmleElementPrivateFtpAcctId)
xmleElementPrivatePassword = myXmlDocument.CreateElement("PrivatePassword-" & strId)
xmleElementPrivatePassword.InnerText = strPrivatePassword
xmleUserNameRoot.AppendChild(xmleElementPrivatePassword)
xmleElementPublicFtpAcctId = myXmlDocument.CreateElement("PublicFtpAccountId-" & strId)
xmleElementPublicFtpAcctId.InnerText = strPublicFtpAcctId
xmleUserNameRoot.AppendChild(xmleElementPublicFtpAcctId)
xmleElementPublicPassword = myXmlDocument.CreateElement("PublicPassword-" & strId)
xmleElementPublicPassword.InnerText = strPublicPassword
xmleUserNameRoot.AppendChild(xmleElementPublicPassword)
' Get the first User.
' ----> Can't see to get the first user. I tried a number of the XmlDocument methods with no sucess.
' Add the whole User Name="#" group after this first User.
' Save in place.
myXmlDocument.Save("MyFiLeZillaforadding.xml")
Console.WriteLine("The XML file was saved successfully.")
...问候
答案 0 :(得分:1)
看起来您正在创建用户&#39;要添加到文档中的新数据的节点,然后将其附加到文档的末尾:
xmleUserNameRoot.SetAttribute("Name", strId)
myXmlDocument.DocumentElement.AppendChild(xmleUserNameRoot)
您需要做的是在现有文档中找到Users
节点,然后将新的User
节点添加到该节点。
为此,您将获取子节点,然后找到Users
和.appendChild
的节点。
documentation on the XmlDocumentClass应该会有所帮助。
修改强>
因此,了解xml文件的结构,并且Users
节点是文档的最后一个子节点的子节点,我能够使用以下代码访问该节点:
Dim docNodes As XmlNodeList = myXmlDocument.LastChild.ChildNodes()
For Each node As XmlNode In docNodes
If node.Name = "Users" Then
node.AppendChild(xmleUserNameRoot)
End If
Next
或者,因为知道Users
节点是FileZillaServer
节点的最后一个节点,而节点又是文档的最后一个节点,所以你可以简单地执行
myXmlDocument.LastChild.LastChild.AppendChild(xmleUserNameRoot)
这不是很灵活,但它有效!
答案 1 :(得分:0)
此行将选择myXmlDocument中的第一个User元素到xmlNodeFirstUser
Dim xmlNodeFirstUser As XmlNode = myXmlDocument.SelectSingleNode("//User")
定义要在此处插入的节点
Dim xmlNodeToInsert As XmlNode
这应该在xml doc
中的第一个User Element之后插入要插入的NodemyXmlDocument.InsertAfter(xmlNodeToInsert, xmlNodeFirstUser)
在.Net,XmlDocument
和SelectSingleNode(string xpathExpression)
中结合使用InsertAfter(XmlNode toInsert, XmlNode insertAfterMe)
方法,您应该能够轻松地操纵和遍历Xml文档:Please look here
您还应该查看XPath表达式,以便选择所需的节点。